简体   繁体   English

在跨域服务器的GET请求中包含cookie

[英]Including cookies in GET request to cross-domain server

I'm trying to send an XHR via pure Javascript to a cross-domain server, while including the cookie values that I've generated on my domain. 我正在尝试通过纯Javascript将XHR发送到跨域服务器,同时包括我在域中生成的Cookie值。

I am also working on an embedded system that does not have a same-origin policy enabled. 我还在未启用同源策略的嵌入式系统上工作。

I've attempted a few different configurations that I've found on SO but none have worked so far. 我尝试了一些在SO上发现的不同配置,但到目前为止都没有用。

For example, here is my GET request: 例如,这是我的GET请求:

Remote Address: XXX.XXX.XXX.XXX:80  
Request URL: http://someotherdomain.com/api?param1=value&param2=value2  
Request Method: GET

Accept:*/*  
Accept-Encoding:gzip,deflate,sdch  
Accept-Language:en-US,en;q=0.8  
Connection:keep-alive  
Cookie:OTHERSITESCOOKIE=somevalue  
Host:someotherdomain.com  
Referer:http://myexample.com/mypage 

And here are some of the response headers that I can see: 这是我可以看到的一些响应头:

Access-Control-Allow-Credentials:true  
Access-Control-Allow-Origin:*  
Content-Length:79  
Content-Type:application/json  

So the problem for me is that the remote server is expecting certain values in the "Cookie" header, but I cannot set it directly in the request. 因此,对我来说,问题是远程服务器在“ Cookie”标头中期望某些值,但是我不能直接在请求中设置它。 I tried setting the cookie for my domain but it was not sent with the request, and I cannot create a cookie for the remote domain. 我尝试为我的域设置cookie,但未随请求一起发送,并且无法为远程域创建cookie。

Does anyone know what I'm missing to be able to include my own domain's cookies in the request? 有人知道我想在请求中包含我自己域的cookie吗? Thanks! 谢谢!

You can not set the Cookie header for an ajax request, you will get an Illegal warning if you try. 您无法为ajax请求设置Cookie标头,如果尝试将收到非法警告。 One option is to set a different header (ie, Ajax-Cookie) and then have them parse it on the server side. 一种选择是设置一个不同的标头(即Ajax-Cookie),然后让它们在服务器端解析它。

Pure JavaScript: 纯JavaScript:

xmlhttp.setRequestHeader( 'Ajax-Cookie', document.cookie );

jquery equivalent: jQuery等效:

$.ajax({
        url: cross-domain-url,
        headers: { 'Ajax-Cookie' : document.cookie }
        ...
});

If you are using php you can use http_parse_cookie to get the cookies values. 如果您使用的是php,则可以使用http_parse_cookie来获取cookie值。

$_AJAX_COOKIES = http_parse_cookie( $_SERVER[ 'HTTP_AJAX_COOKIE' ] )[ 'cookies' ];

EDIT: 编辑:

Here is a list of headers you can not set: accept-charset, accept-encoding, access-control-request-headers, access-control-request-method, connection, content-length, content-transfer-encoding, cookie, cookie2, date, expect, host, keep-alive, origin, referer, te, trailer, transfer-encoding, upgrade, via 这是您无法设置的标头的列表:accept-charset,accept-encoding,access-control-request-headers,access-control-request-method,连接,content-length,content-transfer-encoding,cookie,cookie2 ,日期,期望,主机,保持活动状态,来源,引荐来源网址,te,预告片,传输编码,升级,通过

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

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