我正在使用$ http(angular)併發布到我無法控制的遠程網站。
登錄時,它會給我一個我想閱讀的'site_session'cookie。 請注意,由於在Cordova應用程序中的移動設備上運行,我可以調用遠程網站(跨域)。從XHR請求/響應(PhoneGap/Cordova)讀取/寫入Cookie
我嘗試了各種網絡上找到的解決方案,沒有任何運氣。
我使用的是Angular 1.3.13,所以$timeout
之後的$cookies
應該可以工作,但不會。
我嘗試了withCredentials = true
這個技巧,無論是在$ httpProvider.defaults還是在$ http調用的配置本身:沒有運氣。
$ cookies似乎死了,並在每個範圍內重置。
例如:
$http({
method: 'POST',
url: 'http://distantsite.com/login.php',
data: 'username=test&password=test',
headers: {
'Accept': 'text/html',
'Cookie': "site_session=abcd", // this will be blocked: refused to set unsafe header
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Credentials': true,
},
withCredentials: true,
}).success(function(response, status, headers){
$cookies.myTest1 = 'test1';
$log.info('cookies: ', $cookies); // Object {myTest1: "test1"}
$timeout(function(){
$cookies.myTest2 = 'test2';
$log.info('cookies after timeout: ', $cookies); // Object {myTest2: "test2"}, myTest1 has disappeared!
});
});
輸出:
cookies: Object {myTest1: "test1"}
cookies after timeout: Object {myTest2: "test2"}
我沒有得到任何真正的餅乾,只有最後一個我訂的......
我完全沒有解決方案。有沒有人有一種有效的工作方式來檢索在Cordova應用程序中從Angular調用遠程(跨域)網站的cookie?
如果可能的話,我想保留$ http,但我不介意使用別的東西,只要它從客戶端(瀏覽器)工作。
你是什麼意思遠處的網站? –
我想OP代表來自HTTP響應的'Set-Cookie'頭文件。我想你可以解析標題。 – davidshen84
@NexusDuck遙遠的網站意味着不是一個本地主機請求,@ davidshen84不幸的是,頭文件沒有'Set-Cookie',爲了安全或者因爲它實際上是在成功登錄後重定向到另一個頁面,最後一個調用沒有'Set-Cookie'頭文件。 – Thomas