保持cookie的跨域Ajax请求后,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.请求是这样...

运行 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.

jQuery:发送凭据与跨域职位?

阅读全文

相关推荐

最新文章