【问题标题】:CORS, IIS7 and PHP - Access-Control-Allow-Origin errorCORS、IIS7 和 PHP - Access-Control-Allow-Origin 错误
【发布时间】:2014-08-11 04:50:48
【问题描述】:

我正在尝试允许另一个主机(本地主机,如 javascript.dev)对该主机进行 xhr,它是一个 IIS7,如果我 curl -I 它,这是标题:

HTTP/1.1 200 OK
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Server: Microsoft-IIS/7.0
X-Powered-By: PHP/5.3.28
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers: *
X-Powered-By: ASP.NET
Date: Fri, 20 Jun 2014 12:09:33 GMT

这是curl -v -X OPTIONS 的标题:

* About to connect() to www2.xxxxxxxxxxxx.com.br port 80 (#0)
*   Trying 200.98.xxx.100...
* Connected to www2.xxxxxxxxxxxx.com.br (200.98.xxx.100) port 80 (#0)
> OPTIONS /jobs/xxxxxxx/user/ HTTP/1.1
> User-Agent: curl/7.30.0
> Host: www2.xxxxxxxxxxxx.com.br
> Accept: */*
> 
< HTTP/1.1 200 OK
< Allow: OPTIONS, TRACE, GET, HEAD, POST
* Server Microsoft-IIS/7.0 is not blacklisted
< Server: Microsoft-IIS/7.0
< Public: OPTIONS, TRACE, GET, HEAD, POST
< X-Powered-By: ASP.NET
< Date: Fri, 20 Jun 2014 13:01:25 GMT
< Content-Length: 0

我使用 php 更改了Access-Control-Allow-Origin,但是当我执行 xhr 时,无论是否使用 jquery,这是我得到的错误:

XMLHttpRequest cannot load http://www2.xxxxxxxx.com.br/jobs/xxxxxx/user/. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://javascript.dev' is therefore not allowed access. 

记录,我要解决的其他步骤:

我在上面的答案中添加了代码到我的 web.config 并得到了这个错误:

XMLHttpRequest cannot load http://www2.madeinweb.com.br/jobs/eminhasaude/user. 
Request header field Content-Type is not allowed by Access-Control-Allow-Headers. 

因为Access-Control-Allow-Headers 不接受通配符*。解决:

<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />

【问题讨论】:

  • 那么当您发送OPTIONS /jobs/xxxxxx/user/ 时,它也会返回这些标头吗?想必它对那个地址的POST 也有同样的作用?浏览器调试工具的网络选项卡中会显示什么?
  • 我添加了 OPTIONS 的标题
  • 我认为这可能是您的问题 - 浏览器在执行实际操作之前执行 OPTIONS 请求。您的 OPTIONS 没有Access-Control-Allow-Origin。
  • 好的,真的认为这是问题所在,但我可以用我的 php 应用程序处理对 OPTIONS 请求的响应吗?如果不能,我怎么能在 IIS7 中做到这一点?

标签: javascript php iis-7 xmlhttprequest cors


【解决方案1】:

根据 cmets,您在提交 OPTIONS 请求时似乎缺少 Access-Control-Allow-Origin 标头。根据this 文章,应该是一个简单的例子,将以下代码添加到您的 PHP 页面中......

<?php
header('Access-Control-Allow-Origin: *');
?>

如果这仍然不起作用,那么您应该检查 PHP 的 IIS 处理程序映射(请参阅here)并确保 OPTIONS 是允许的动词。希望这能起到作用!

This 文章还表明您可以完全跳过修改 PHP,只需将以下内容添加到您的 web.config:

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Headers" value="*" />
      <add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" />
      <add name="Access-Control-Max-Age" value="1000" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

请注意,这将打开整个网站,而不仅仅是一页...

【讨论】:

  • 好的,添加到网络配置中,现在 OPTIONS 请求的标题是:Access-Control-Allow-Origin: * &lt; Access-Control-Allow-Headers: * &lt; Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS 但我仍然收到错误,现在这个错误:XMLHttpRequest cannot load http://www2.madeinweb.com.br/jobs/eminhasaude/user. Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
  • 看起来Access-Control-Allow-Headers 不接受通配符...stackoverflow.com/questions/13146892/…。尝试仅添加 Content-Type 而不是 *
  • 是的,我解决了这个问题:&lt;add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" /&gt;
  • 从 php 控制器中删除标头并在 web.config 中进行上述更改对我有帮助。非常感谢。
猜你喜欢
  • 2016-09-30
  • 2021-06-17
  • 1970-01-01
  • 2015-08-17
  • 2021-05-12
  • 1970-01-01
  • 2014-02-18
  • 2021-12-02
  • 2019-02-07
相关资源
最近更新 更多