【问题标题】:How can i resolve this error我该如何解决这个错误
【发布时间】:2016-09-09 04:35:40
【问题描述】:

我正在尝试使用 AJAX 调用 WCF 服务,下面是我的代码:

$.ajax({
    url: "http://localhost/TestingServices/Service1.svc/GetData"
    data: "{'value:1}",
    type: "POST",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        alert(data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus);
    }
});

但执行后它给了我以下错误:

XMLHttpRequest cannot load 
  http://localhost/TestingServices/Service1.svc/GetData.Response to 
  preflight request doesn't pass access control check: 
  No 'Access-Control-Allow-Origin' header is present on 
  the requested resource. Origin 'null' is therefore not 
  allowed access. The response had HTTP status code 404.

谁能帮我解决这个问题?

【问题讨论】:

标签: javascript jquery ajax html cordova


【解决方案1】:

尝试 2 在服务项目的 web.config 中添加 HttpProtocols

    <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
</system.webServer>

【讨论】:

    【解决方案2】:

    请试试这个....

    $.ajax({
    type: "GET",
    url: "http://localhost/TestingServices/Service1.svc/GetData",
    dataType: "jsonp",
    success: readData(data),
    error: function (xhr, ajaxOptions, thrownError) {
      alert(xhr.status);
      alert(thrownError);
     }
    })
    

    【讨论】:

      猜你喜欢
      • 2016-07-21
      • 2015-02-28
      • 2013-08-16
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多