weixin_33716557 2016-05-13 05:58 采纳率: 0%
浏览 72

我该如何解决这个错误

I am trying to call WCF Services using AJAX, below is my code:

$.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);
    }
});

But after execution it gives me following error:

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.

Can anyone help me how to resolve this ?

  • 写回答

2条回答 默认 最新

  • weixin_33704234 2016-05-13 06:22
    关注

    Try 2 Add HttpProtocols in web.config of your service project

        <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>
    
    评论

报告相同问题?