保持cookie的跨域Ajax请求后, [英] Keeping the cookie after a cross-domain ajax request

查看:215
本文介绍了保持cookie的跨域Ajax请求后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行 10.0.0.1 尝试验证它的用户提供跨域Ajax调用JavaScript的应用程序。

A javascript application running on 10.0.0.1 tries to authenticate it's users with cross-domain ajax calls.

请求是这样的:

function test(again){
  $.ajax({
    type: 'GET',
    url: 'http://example.com/userinfo',
    dataType: 'json',
    success: function(userinfo){
      if(again)
        test(false);}});}
test(true);

来自服务器的第一个响应试图设置cookie:

The first response from the server tries to set a cookie:

Access-control-allow-origin:http://10.0.0.1
Set-Cookie:PHPSESSID=uuj599r4k1ohp48f1poobil665; expires=Sat, 28-Jan-2012 17:10:40 GMT; path=/

但是,第二个请求不包括该cookie,也不做任何其他的Ajax请求到该域。

But the second request does not include this cookie, nor do any other ajax requests to that domain.

我不是读了另一个域的饼干,我只是想在其他领域的应用才能够设置和读取它自己的cookie。

I am not trying to read the cookie for another domain, I just want the application on the other domain to be able to set and read its own cookie.

这可能吗?

我在Chrome和Firefox 9测试了。

I have tested in Chrome and Firefox 9.

推荐答案

只要你使用的是支持CORS浏览器,在AJAX请求饼干应该工作。但是,你必须设置 withCredentials XMLHtt prequest 为true。

As long as you are using a browser which supports CORS, cookies on the AJAX request should work. But you must set withCredentials on the XMLHttpRequest to true.

请参阅:的withCredentials属性

我不使用JQuery,但这里有专门处理通过JQuery设置 withCredentials 的问题。

I don't use JQuery but here's a question that deals specifically with setting withCredentials via JQuery.

<一个href="http://stackoverflow.com/questions/2054316/jquery-sending-credentials-with-cross-domain-posts">jQuery:发送凭据与跨域职位?

这篇关于保持cookie的跨域Ajax请求后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆