【发布时间】: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