傳遞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});

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