Access-Control-Allow-Origin 在 IIS 中,但无法正常工作
Posted
技术标签:
【中文标题】Access-Control-Allow-Origin 在 IIS 中,但无法正常工作【英文标题】:Access-Control-Allow-Origin is in IIS, but not working 【发布时间】:2016-08-31 10:06:53 【问题描述】:我刚刚创建了我的第一个 WCF 服务,我想与其他客户共享它。我已经在 IIS、Web.config 和 Global.asax 中添加了 Access-Control-Allow-Origin
标头,但仍然不允许远程客户端访问该服务。我已经在 Chrome 和 IE(我们组织支持的浏览器)中进行了测试。我的错误是“对预检请求的响应未通过访问控制检查:请求的资源上不存在 'Access-Control-Allow-Origin' 标头。”
在 Web.config 中:
<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="POST,GET,OPTIONS"/>
</customHeaders>
</httpProtocol>
在 Global.asax 中:
protected void Application_BeginRequest(object sender, EventArgs e)
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
//These headers are handling the "pre-flight" OPTIONS call sent by the browser
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
他们在 IIS 中:
这是试图访问服务的 javascript。它可以在本地工作,但不能在远程工作:
$.ajax(
type: "POST",
url: "http://localhost/wsSamwise/Service1.svc/GetTime",
data: '"UserName": "' + userName + '"',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data)
$("#results").html(data.GetTimeResult);
console.log(data);
);
所有这些想法都来自各种 *** 线程,但没有一个对我有用。我错过了什么?
编辑:我刚刚在我的本地主机和远程浏览器中检查了响应的标题。我在本地主机的响应中看到了 Access-Control-Allow-Origin
标头,但在远程浏览器的响应中没有看到。好像没有发送一样。
来自我的本地主机的响应标头:
Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Methods:POST,GET,OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Allow-Origin:*
Cache-Control:private
Content-Length:82
Content-Type:application/json; charset=utf-8
Date:Thu, 05 May 2016 16:01:28 GMT
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
来自远程浏览器的响应:
Allow:OPTIONS, TRACE, GET, HEAD, POST
Content-Length:0
Date:Thu, 05 May 2016 16:00:09 GMT
Public:OPTIONS, TRACE, GET, HEAD, POST
Server:Microsoft-IIS/7.5
X-Powered-By:ASP.NET
编辑 2:伙计们,也许这完全是 IIS 问题。我刚刚向 IIS 添加了一个自定义标头。它显示在本地主机浏览器中,但不在客户端上。远程浏览器中缺少 Body-Count
标头,但我在本地浏览器上看到了它。
【问题讨论】:
具体来说,它是如何不起作用的?是否超时?给出错误信息?空响应? 编辑到包含错误。 你检查过this link吗? 我没见过那个。它似乎为许多其他教程和线程中看似简单的解决方案增加了很多复杂性。我什至不确定在我的服务中从哪里开始实施它。 但是您的其他客户端使用 JQuery 客户端?或者是适合您的测试。因为如果您的客户端使用 Java 或 C# 等,问题就不一样了... 【参考方案1】:确保 Ajax URL 正确。
在发布的原始代码中,我的 Ajax 请求的 URL 是查看 localhost 而不是托管服务的机器的 FQDN。这就是问题所在。当然,如果服务不是托管在 URL 上,那么 Ajax 请求将不起作用。我被错误消息分心了,以至于我错过了明显而简单的解决方案。
【讨论】:
【参考方案2】:使用 JSONP 是从不同领域(跨域请求共享)工作的最经典和标准。 然后查看 Access-Control-Allow-Origin 以添加特定的限制。
像这样使用 JSONP: 新的 JSONP 功能通过 WebHttpBinding 公开。 CustomersService 的配置如下所示:
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<services>
<service name="ServiceSite.CustomersService">
<endpoint address="" binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJsonP" contract="ServiceSite.CustomersService"
behaviorConfiguration="webHttpBehavior"/>
</service>
</services>
通过 jQuery 使用 JSONP
// Get the JsonP data
$.getJSON('http://localhost:65025/CustomersService.svc/GetCustomers?callback=?', null, function (customers)
alert('Received ' + customers.length + ' Customers');
);
来源: How to natively enable JSONP for existing WCF service?
【讨论】:
这将取代客户端应用程序中的$.ajax()
?我认为$.ajax()
是与 Web 服务交互的首选方式。
当我尝试此操作时,我收到错误消息,即根据其数据类型“endpointBehaviorConfugurationType”,“webHttpBehavior”无效。当我运行它时,我得到一个 JS 错误“方法不允许”。
试试这个文档:bendewey.com/index.php/186/using-jsonp-with-wcf-and-jquery
发布您的网络服务代码和web.config。 $.ajax 是最冗长的。 learn.jquery.com/ajax/working-with-jsonp
我担心用 14 吨代码过度填充原始帖子。服务中是否有特定的部分可以展示?以上是关于Access-Control-Allow-Origin 在 IIS 中,但无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章