2015-06-25 126 views
9

我对另一个服务API进行了AJAX调用,然后应该返回一个将在我的浏览器中设置的cookie,以允许我调用其余的API调用。Set-Cookie header未在Chrome中设置Cookie

但是,虽然响应头包含一个'Set-Cookie'头,但实际上没有设置Cookie。我正在使用Google Chrome。

这里是响应头:

Access-Control-Allow-Origin:* 
Cache-Control:no-cache 
Connection:keep-alive 
Content-Encoding:gzip 
Content-Length:37 
Content-Type:application/json 
Date:Thu, 25 Jun 2015 18:27:37 GMT 
Expires:Thu, 25 Jun 2015 18:27:36 GMT 
Server:nginx/1.4.6 (Ubuntu) 
Set-Cookie:sessionid=67cb9796aa794a4975b28876ea6dd3d5; expires=Thu, 09-Jul-2015 18:27:37 GMT; httponly; Max-Age=1209600; Path=/ 
Vary:Cookie 

这里是AJAX调用:

$.ajax({ 
      type: "POST", 
      crossDomain: true, 
      contentType: 'text/plain', 
      data: data, 
      url: urlhere 
      success: function(result, status, xhr){ 
       console.log('hi'); 
       console.log(xhr.getAllResponseHeaders()); 
      }, 
      error: function(xhr){ 
       console.log(xhr.status); 
       console.log(xhr.statusText); 
      } 
}); 

的铬资源页面还显示,没有任何cookie被设定。任何和所有的帮助将不胜感激!

+1

你看了这里:http://stackoverflow.com/questions/16019135/set-cookie-without-page-refresh – testing123

回答

7

您需要将withCredentials附加到您的XHR调用中,此答案显示了如何使用jQuery来做到这一点。 https://stackoverflow.com/a/7190487 没有额外的标志,浏览器将不会接受set-cookie标头。