调用wcf服务时警告CORS request failed如何解决?

How to solve warning CORS request failed when call wcf service?

我在 IIS 7.5 上发布我的服务,但我无法在 Ajax 中调用我的服务 ,应该注意的是,我在我的服务上设置了所有交叉设置,当我用 Ajax 调用服务时,我的控制台收到了两个警告:

1-Cross-Origin 请求被阻止:同源策略不允许读取位于 http://84.241.41.5:8000/Sale.svc/GetDataReportSaleOfMonth 的远程资源。 (原因:CORS header 'Access-Control-Allow-Origin' 不匹配'*, *')。

2-Cross-Origin 请求被阻止:同源策略不允许读取位于 http://84.241.41.5:8000/Sale.svc/GetDataReportSaleOfMonth 的远程资源。 (原因:CORS 请求失败)。

URL of my service activate in address

我的 Ajax 代码是:

$.ajax({ 
 type: "Get",
 url: "http://84.241.41.5:8000/Sale.svc/GetDataReportSaleOfMonth", 
 contentType: "application/json;charset-uf8", // content type sent to server
 dataType: "json", //Expected data format from server
 crossDomain: true,
 success: function(response) {//On Successfull service call
            ServiceSucceeded(msg);
        },
 error: function (response, errorText) {
        ServiceFailed(response);
        }// When Service call fails
    });
});

function ServiceFailed(result) {
        alert('Service call failed: ' + result.status + '' + result.statusText);

    }

 function ServiceSucceeded(result) {
         alert("ok");

        }

请帮我看看我在 Ajax 中的服务出错的原因是什么。

运行 您在 Chrome 中的代码产生以下错误:

XMLHttpRequest cannot load http://84.241.41.5:8000/Sale.svc/GetDataReportSaleOfMonth. The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Origin 'null' is therefore not allowed access.

当我向您发布的 API 发出请求时,我看到了以下 header:

Cache-Control: private
Content-Length: 45
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Access-Control-Allow-Origin: *
Access-Control-Request-Method: POST,GET,PUT,DELETE,OPTIONS
Access-Control-Allow-Headers: accept,Content-Type
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS
Access-Control-Allow-Headers: accept,Content-Type
Date: Wed, 24 Jun 2015 03:56:11 GMT

请注意 Access-Control-Allow-Origin: * header 出现了两次。查看您的服务器实现,并尝试删除其中之一。