2015-09-23 73 views
2

我在我的頁面http://vista.local/上加載了javascript。該腳本發送POST請求一個web服務http://api.vista.local/sessions其設定在Cookie未設置來自跨域AJAX請求

Accept-Ranges:bytes 
Access-Control-Allow-Credentials:true 
Access-Control-Allow-Headers:Accept, Origin, Content-Type, Cookie 
Access-Control-Allow-Methods:GET,POST 
Access-Control-Allow-Origin:http://vista.local 
Age:0 
Cache-Control:public, max-age=1 
Connection:keep-alive 
Content-Length:36 
Content-Type:application/json 
Date:Sun, 23 Aug 2015 06:00:45 GMT 
Server:Apache-Coyote/1.1 
Set-Cookie:vista-session=c07c4d25-5c6e-47a3-9bbc-60f7d122075a;Domain=.vista.local;Path=/;HttpOnly 
Set-Cookie:vista-loggedin=true;Domain=.vista.local;Path=/; 
Set-Cookie:[email protected];Domain=.vista.local;Path=/;HttpOnly 
Set-Cookie:vista-session-dummy=c07c4d25-5c6e-47a3-9bbc-60f7d122075a;Version=1 
Set-Cookie:vista-secured-session=08239195-e95e-4f42-adeb-5d9c745ca853;Domain=.vista.local;Path=/;Secure;HttpOnly 
Vary:Accept-Encoding,User-Agent 
X-UA:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36 

我嘗試不同類型的Cookie ..僅Http,非僅Http而不域的響應被設置的cookie,與結構域。什麼都沒有

我JS/jQuery代碼請求

function login(formdata) { 
    $.ajax({ 
     url : api.sessions, 
     method : "POST", 
     data : JSON.stringify(formdata), 
     dataType : 'text', 
     contentType: 'application/json', 
     success : function(data) { 
      console.log("yeeeii") 
     }, 
     error: function(jqXHR, textStatus, errorThrown) { 
      $(".error").html(textStatus); 
     } 
    }); 
} 

回答

6

設置withCredentials

... 
method : "POST", 
xhrFields: { 
    withCredentials: true 
}, 
... 
+0

太謝謝你了!工作就像一個魅力:) – user699681

+0

@ user699681如果它的作品像一個魅力,那麼爲什麼你不選擇它? –