【问题标题】:How to solve warning CORS request failed when call wcf service?调用 wcf 服务时如何解决警告 CORS 请求失败?
【发布时间】:2015-06-24 03:46:40
【问题描述】:

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

1-Cross-Origin Request Blocked:同源策略不允许读取http://84.241.41.5:8000/Sale.svc/GetDataReportSaleOfMonth 的远程资源。 (原因:CORS 标头 'Access-Control-Allow-Origin' 不匹配 '*, *')。

2-跨域请求被阻止:同源策略不允许读取位于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 中的服务出错的原因。

【问题讨论】:

    标签: c# jquery .net ajax wcf


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 2012-01-28
      相关资源
      最近更新 更多