我想使用 jQuery v1.8.3 进行跨域 GET ajax 请求。
这是我的设置:
我有一个服务器,它为每个请求
domain1.dev
发出这些 CORS 标头:Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: *
Access-Control-Allow-Headers: *
我有一个 Web 应用程序,它从被调用
domain2.dev
加载 javascript 。domain1.dev
domain1.js
在这个
domain1.js
文件中,我发出这个 AJAX 请求:jQuery.ajax('http://domain1.dev/path', { headers: {'Accept': 'application/json'}, xhrFields: { withCredentials: true }, statusCode: { 200: function(data, textStatus, jqXHR) { // some logic }, 401: function(data, textStatus, jqXHR) { // some other logic } } })
我从加载页面
domain2.dev
。发出 AJAX 请求(没有预检)并且成功(状态代码 200)但 firebug 仍然以红色显示它(chrome 显示“已取消”),我不明白为什么。
任何的想法?
编辑:
由 发送的响应标头domain1.dev
:
Access-Control-Allow-Cred... true
Access-Control-Allow-Head... Accept, Accept-Encoding, Accept-Language, Connection, Cookie, Host, Origin, Referer, User-Agent
Access-Control-Allow-Meth... GET
Access-Control-Allow-Orig... *
Cache-Control private
Connection keep-alive
Content-Type application/json
Date Thu, 22 Nov 2012 22:24:44 GMT
Server nginx/1.2.3
Transfer-Encoding chunked
X-Powered-By PHP/5.4.6
x-debug-token 50aea62c91ee6
而请求标头是:
Accept application/json
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection keep-alive
Cookie obfuscated=uk40mmmqbffues5cpn6omj6mf0; __utma=126865951.1486960365.1353615466.1353615466.1353619027.2; __utmz=126865951.1353615466.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmc=126865951
Host domain1.dev:8080
Origin http://domain2.dev:8080
Referer http://domain2.dev:8080/app_dev.php
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/17.0 Firefox/17.0