简体   繁体   中英

how to call webservices using ajax in asp.net mvc 5

I am working on a asp.net mvc 5 and I want to use web api in it and I use a jquery library and below code.but I didnot get proper answer .it returns always fail but I can see the output in the browser.please help 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>

I can see the output in the browser

You will not be able to make an AJAX call to this URL because of the Same Origin Policy (Yes you can copy and paste the URL in the browser and get a response, but you cannot access it from javascript).

Same origin policy states that AJAX calls will only be allowed if they are coming from the same domain, you are doing something like this:

making an AJAX call from http://localhost... to http://charter724... - this is not allowed

If you inspect the browser console you will see an error similar to the one below:

XMLHttpRequest cannot load http://charter724.ir/webservice/get_city_a.php?id=367&key=EmlyCD8JOWX22cL8IuNBju5K5 . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:10585 ' is therefore not allowed access

The only way around this is for you to contact the company exposing the charter724 service and ask them to enable CORS(Cross Origin Resource Sharing) and allow all requests coming from your domain to access their service calls.

Alternatively you can access the service call using HttpClient and get the JSON response.If you really wanted to you could even expose your own set of services which talk to the external service and access your service calls from 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM