i working on asp.net mvc 5 , want use web api in , use jquery library , below code.but didnot proper answer .it returns fail can see output in browser.please me
<!doctype html> <html> <head> <title></title> <meta charset="utf-8" /> <script src="js/jquery-1.12.4.js"></script> <script> function ftoken() { $.ajax({ url: 'http://charter724.ir/webservice/get_city_a.php?id=367&key=emlycd8jowx22cl8iunbju5k5', // data: { }, type: 'get', datatype: 'json', success: function (data) { alert('success') // alert(data.data) //if (country_id != "" && zone_code != "" && duration != "" && birthday != "") { // fprice(data.token); //} //$.each(data, function (idx, result) { // alert(result.data); //}) }, error: function (x, y, z) { alert('fail') //alert(x.responsetext); //alert(z); } }); } </script> </head> <body> <input type="submit" name="name" onclick="ftoken();" value="click " /> </body> </html>
you not able make ajax call url because of same origin policy(yes can copy , paste url in browser , response, cannot access javascript).
same origin policy states ajax calls allowed if coming same domain, doing this:
making ajax call http://localhost...
http://charter724...
- this not allowed
if inspect browser console see error similar 1 below:
xmlhttprequest cannot load http://charter724.ir/webservice/get_city_a.php?id=367&key=emlycd8jowx22cl8iunbju5k5. no 'access-control-allow-origin' header present on requested resource. origin 'http://localhost:10585' therefore not allowed access
the way around contact company exposing charter724
service , ask them enable cors(cross origin resource sharing) , allow requests coming domain access service calls.
alternatively can access service call using httpclient
, json response.if wanted expose own set of services talk external service , access your service calls java script:
public async system.threading.tasks.task<actionresult> index() { string url = "http://charter724.ir/webservice/get_city_a.php?id=367&key=emlycd8jowx22cl8iunbju5k5"; system.net.http.httpclient client = new system.net.http.httpclient(); system.net.http.httpresponsemessage response = await client.getasync(url); response.ensuresuccessstatuscode(); string responsebody = await response.content.readasstringasync(); return view(); }
Comments
Post a Comment