简体   繁体   English

jQuery跨域请求

[英]jQuery Cross Domain Request

I'm trying to get a cross domain request to work. 我正在尝试获得跨域请求才能工作。 The relevant code is as follows: 相关代码如下:

var promise = $.ajax({
    type: 'GET',
    url: registerUrl,
    dataType: 'json',
    cache: false,
    crossDomain: true,
    xhrFields: { withCredentials: true },
    contentType: 'application/json'
});

I control the calling and receiving server, and this is response that I am getting: 我控制呼叫和接收服务器,这是我得到的响应:

Raw request (courtesy of Fiddler): 原始请求(由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

Raw response: 原始回应:

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)

Now, my understanding is that Allow-Origin: * for this preflight should be enough for Firefox / Chrome to let the request continue. 现在,我的理解是,此预检的Allow-Origin: *应该足以使Firefox / Chrome继续请求。 Oddly enough, IE does no preflight for this request, and it works (despite throwing up a warning that it is not following the CORS specification). 奇怪的是,IE不会对此请求进行预检,并且可以正常工作(尽管会发出警告,表明它未遵循CORS规范)。

Am I missing something here? 我在这里想念什么吗? I've tried setting every value on the ajax request I can think of, and the server looks to be responding with the appropriate response to the OPTIONS request. 我试过在我能想到的ajax请求上设置每个值,并且服务器看起来正在以对OPTIONS请求的适当响应进行响应。

The specific error message from firefox: 来自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.

What am I doing wrong? 我究竟做错了什么? What can be changed to fix this? 可以更改以解决此问题?

EDIT: New request / response fields: 编辑:新的请求/响应字段:

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

Response: 响应:

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)

Now, fiddler also records the actual request: 现在,提琴手还记录了实际的请求:

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

And the response: 以及响应:

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"}

However, firefox seems to be blocking the request and thus the ajax call always hits my fail handler. 但是,firefox似乎阻止了该请求,因此ajax调用总是命中我的失败处理程序。 This seems odd... 这似乎很奇怪...

It looks like your request is asking for permission to send a content-type request header, and it requires permission to send credentials. 看起来您的请求正在请求发送内容类型请求标头的权限,并且它需要发送凭据的权限。

Your request headers appear to be correct as far as I can tell. 据我所知,您的请求标头似乎是正确的。 However your response should contain two more headers: 但是,您的响应应包含两个以上的标头:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:content-type

Here is an example of a preflight request from another site I've worked on, and the preflight response: 这是来自我工作过的另一个站点的预检请求和预检响应的示例:

Here is the preflight option requestion headers 这是预检选项请求标头

Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, applicationname
Access-Control-Request-Method:GET
Cache-Control:no-cache
Connection:keep-alive
Cookie:*sanitized cookie data*
Host:sanitized.domain.com
Origin:https://sanitized.domain.com
Pragma:no-cache
Referer:https://sanitized.domain.com/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36

Subsequently here is the preflight response headers: 随后是预检响应头:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:applicationname
Access-Control-Allow-Origin:https://sanitized.domain.com
Cache-Control:no-cache
Connection:keep-alive
Content-Length:0
Date:Thu, 10 Jul 2014 20:19:25 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/8.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET

Notice the addition of the Access-Control-Allow-Credentials, and Access-Control-Allow-Headers headers. 请注意,添加了Access-Control-Allow-Credentials和Access-Control-Allow-Header标头。 Also notice my request sent "accept" in the Access-Control-Request-Headers header, and the response didn't contain "accept". 还要注意,我的请求在Access-Control-Request-Headers标头中发送了“接受”,并且响应中不包含“接受”。 This is because "accept" is considered a simple header as defined here: http://www.w3.org/TR/cors/#simple-header . 这是因为“接受”被视为此处定义的简单标头: http : //www.w3.org/TR/cors/#simple-header Additionally content-type qualifies as a simple header only when it's value is one of the following: 此外,仅当content-type的值是以下值之一时,它才可以作为简单标头:

  • text/plain 纯文本/
  • multipart/form-data 多部分/格式数据
  • application/x-www-form-urlencoded 应用程序/ x-WWW窗体-urlencoded

In your case since the content type is application/json, you would need a response header that contains "content-type", as displayed above. 在您的情况下,由于内容类型为application / json,因此您将需要一个包含“ content-type”的响应标头,如上所示。

UPDATE: Your request and response that occur after the preflighted request should contain the same CORS headers to prevent the browser from rejecting the request and/or response. 更新:在预检请求之后发生的请求和响应应包含相同的CORS标头,以防止浏览器拒绝请求和/或响应。 At a bare minimum you'll want the response to contain the Access-Control-Allow-Credentials and the Access-Control-Allow-Origin headers. 至少需要让响应包含Access-Control-Allow-Credentials和Access-Control-Allow-Origin标头。

Try setting 尝试设定

dataType:'html' 

( Where is the correct place to enable CORS? ) 启用CORS的正确位置在哪里?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM