小编典典

如何使跨域资源共享 (CORS) 发布请求正常工作

all

我的本地局域网 (machineA) 上有一台机器,它有两个 Web 服务器。第一个是 XBMC 中的内置库(在端口 8080
上)并显示我们的库。第二个服务器是一个 CherryPy python 脚本(端口 8081),我用它来按需触发文件转换。文件转换由来自 XBMC
服务器提供的页面的 AJAX POST 请求触发。

  • 转到显示库的http://machineA:8080
  • 显示库
  • 用户单击发出以下命令的“转换”链接 -

jQuery Ajax 请求

$.post('http://machineA:8081', {file_url: 'asfd'}, function(d){console.log(d)})
  • 浏览器发出带有以下标头的 HTTP OPTIONS 请求;

请求标头 - 选项

Host: machineA:8081
User-Agent: ... Firefox/4.01
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://machineA:8080
Access-Control-Request-Method: POST
Access-Control-Request-Headers: x-requested-with
  • 服务器响应以下内容;

响应标头 - 选项(状态 = 200 OK)

Content-Length: 0
Access-Control-Allow-Headers: *
Access-Control-Max-Age: 1728000
Server: CherryPy/3.2.0
Date: Thu, 21 Apr 2011 22:40:29 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Content-Type: text/html;charset=ISO-8859-1
  • 谈话随即停止。理论上,浏览器应该发出 POST 请求,因为服务器使用正确的 (?) CORS 标头进行响应(Access-Control-Allow-Origin: *)

为了进行故障排除,我还从http://jquery.com发出了相同的 $.post
命令。这是我被难住的地方,来自 jquery.com,post 请求有效,OPTIONS 请求在 POST 之后发送。此交易的标题如下;

请求标头 - 选项

Host: machineA:8081
User-Agent: ... Firefox/4.01
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://jquery.com
Access-Control-Request-Method: POST

响应标头 - 选项(状态 = 200 OK)

Content-Length: 0
Access-Control-Allow-Headers: *
Access-Control-Max-Age: 1728000
Server: CherryPy/3.2.0
Date: Thu, 21 Apr 2011 22:37:59 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Content-Type: text/html;charset=ISO-8859-1

请求标头 - POST

Host: machineA:8081
User-Agent: ... Firefox/4.01
Accept: */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://jquery.com/
Content-Length: 12
Origin: http://jquery.com
Pragma: no-cache
Cache-Control: no-cache

响应标头 - POST(状态 = 200 OK)

Content-Length: 32
Access-Control-Allow-Headers: *
Access-Control-Max-Age: 1728000
Server: CherryPy/3.2.0
Date: Thu, 21 Apr 2011 22:37:59 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Content-Type: application/json

我无法弄清楚为什么相同的请求可以从一个站点工作,但不能从另一个站点工作。我希望有人能够指出我所缺少的。谢谢你的帮助!


阅读 77

收藏
2022-05-30

共1个答案

小编典典

我终于偶然发现了这个链接“ A CORS POST request works from plain javascript, but why not
with jQuery?
”指出 jQuery 1.5.1 添加了

 Access-Control-Request-Headers: x-requested-with

所有 CORS 请求的标头。jQuery 1.5.2 不这样做。另外,根据相同的问题,设置服务器响应标头

Access-Control-Allow-Headers: *

不允许响应继续。您需要确保响应标头特别包含所需的标头。IE:

Access-Control-Allow-Headers: x-requested-with
2022-05-30