【发布时间】:2014-09-01 08:19:51
【问题描述】:
我正在尝试让跨域请求正常工作。相关代码如下:
var promise = $.ajax({
type: 'GET',
url: registerUrl,
dataType: 'json',
cache: false,
crossDomain: true,
xhrFields: { withCredentials: true },
contentType: 'application/json'
});
我控制调用和接收服务器,这是我得到的响应:
原始请求(由 Fiddler 提供):
OPTIONS http://localhost:5080/mportal/registerChat?gsid=abcde&ul=100,200&_=1405022169353 HTTP/1.1
Host: localhost:5080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0
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
Origin: http://localhost:53054
Access-Control-Request-Method: GET
Access-Control-Request-Headers: content-type
Connection: keep-alive
原始响应:
HTTP/1.1 200 OK
Date: Thu, 10 Jul 2014 19:56:11 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Content-Length: 0
Server: Jetty(8.1.4.v20120524)
现在,我的理解是,此预检的 Allow-Origin: * 应该足以让 Firefox / Chrome 继续请求。奇怪的是,IE 没有对此请求进行预检,并且它可以工作(尽管抛出了一个警告,表明它不遵循 CORS 规范)。
我在这里遗漏了什么吗?我已经尝试在我能想到的 ajax 请求上设置每个值,并且服务器看起来正在响应 OPTIONS 请求的适当响应。
来自firefox的具体错误信息:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5080/mportal/registerChat?gsid=abcde&ul=100,200&_=1405022169353. This can be fixed by moving the resource to the same domain or enabling CORS.
我做错了什么?可以改变什么来解决这个问题?
编辑:新的请求/响应字段:
OPTIONS http://localhost:5080/mportal/registerChat?gsid=abcde&ul=100,200&_=1405026511996 HTTP/1.1
Host: localhost:5080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
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
Origin: http://localhost:53054
Access-Control-Request-Method: GET
Access-Control-Request-Headers: content-type
Connection: keep-alive
回复:
HTTP/1.1 200 OK
Date: Thu, 10 Jul 2014 21:08:38 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Credentials: true
Content-Length: 0
Server: Jetty(8.1.4.v20120524)
现在,fiddler 也记录了实际的请求:
GET http://localhost:5080/mportal/registerChat?gsid=abcde&ul=100,200&_=1405026511996 HTTP/1.1
Host: localhost:5080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/json
Referer: http://localhost:53054/
Origin: http://localhost:53054
Connection: keep-alive
然后回应:
HTTP/1.1 200 OK
Date: Thu, 10 Jul 2014 21:08:38 GMT
Content-Type: application/json;charset=ISO-8859-1
Content-Length: 175
Server: Jetty(8.1.4.v20120524)
{"status":true,"host":"<myServerName>","port":1935,"liveHost":"<myServerName>","livePort":5080,"gsid":"abcde","connCount":2,"maxConns":25000,"version":"1.0"}
但是,firefox 似乎阻止了请求,因此 ajax 调用总是命中我的失败处理程序。这看起来很奇怪......
【问题讨论】:
-
这个post 也很有用;阅读与 CORS 请求相关的部分。
-
我使用的是 1.5.2 之后的版本,请求中没有发送
x-requested-with标头。是回复中没有Access-Control-Allow-Headers的问题吗?
标签: javascript jquery ajax cross-domain chat