传递cookie

(适用于用户登录)

如果ajax发生跨域请求,就不会携带存在浏览器端的cookie信息,解决方法:请求参数中将withCredentials为true

$.ajax({
   url: a_cross_domain_url,
   // 将XHR对象的withCredentials设为true
   xhrFields:{
      withCredentials:true
   }});

同时服务器端还需设置 Access-Control-Allow-Origin 为 true

response().setHeader("Access-Control-Allow-Credentials", "true");


angular的传递cookie方法:

$http.post("http://a.domain.com/Api/Product",{ productId: 3 },{

withCredentials: true,

params: { name: "Ray" },

headers: {'Authorization':"这个不重要"}

}).success(function (data) { //TODO});

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章