调用wcf服务时如何解决警告CORS请求失败?

时间:2015-06-24 03:46:40

标签: c# jquery .net ajax wcf

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

1-Cross-Origin请求已阻止:同源策略禁止在http://84.241.41.5:8000/Sale.svc/GetDataReportSaleOfMonth读取远程资源。 (原因:CORS标题'访问控制 - 允许 - 来源'不匹配' *,*')。

2-Cross-Origin Request Blocked:同源策略禁止在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服务时出错的原因。

1 个答案:

答案 0 :(得分:1)

在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发出请求时,我会看到以下标题:

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:*标头出现两次。查看您的服务器实现,并尝试删除其中一个。