简体   繁体   English

如何在ASP.NET MVC 5中使用Ajax调用Web服务

[英]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 我正在asp.net mvc 5上工作,我想在其中使用Web api,并且使用jquery库和下面的代码。但是我没有得到正确的答案。它总是失败,但是我可以在浏览器中看到输出。请帮我

<!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). 由于具有相同的来源政策,您将无法对此URL进行AJAX调用(是的,您可以将URL复制并粘贴到浏览器中并获得响应,但不能从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: 相同来源策略指出,仅当AJAX调用来自同一域时,才允许这样做,您正在执行以下操作:

making an AJAX call from http://localhost... to http://charter724... - this is not allowed http://localhost...http://charter724...进行AJAX调用- 不允许这样做

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 . XMLHttpRequest无法加载http://charter724.ir/webservice/get_city_a.php?id=367&key=EmlyCD8JOWX22cL8IuNBju5K5 No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://localhost:10585 ' is therefore not allowed access 因此,不允许访问源' http:// localhost:10585 '

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. 解决此问题的唯一方法是让您联系提供charter724服务的公司,并要求他们启用CORS(跨源资源共享),并允许来自您域的所有请求访问其服务呼叫。

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: 另外,您可以使用访问服务调用HttpClient ,并得到了JSON response.If你真的想你甚至可能暴露你自己的一套跟外部服务和访问来自Java脚本您的服务电话的服务:

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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