我想從PHP網站上的AngularJS的http調用中獲取標頭。 我打電話的網站的管理員向我保證,CORS已啓用,並且服務器已設置標誌以允許js訪問cookie。 下面是示例代碼:AngularJS無法從PHP獲取HTTP標頭
$http({
method: "POST",
url: 'https://example.com/page',
data: {'Form[foo]': feild1, 'Form[bar]': field2},
headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'}
}).then(function(data, status, headers, config) {
console.log(data.data);
console.log(headers()['Set-Cookie']);
}, function(err) {
console.error('ERR', err);
})
在第一個日誌評論,我可以看到網頁返回。在Chrome Inspector中,我可以點擊頁面回覆並查看標題,包括「Set-Cookie」。 然而,第二個日誌評論返回:
TypeError: undefined is not a function
我已經做了很多尋找答案和反覆試驗測試。例如:
}).then(function(data, status, headers, config) {
console.log(data.headers);
結果 - >
function (name) {
if (!headersObj) headersObj = parseHeaders(headers);
if (name) {
return headersObj[lowercase(name)] || null;
}
return headersObj;
}
除了:
}).then(function(resp) {
console.log(resp.headers["Set-Cookie"]);
結果 - >未定義
console.log(resp.header('Set-Cookie'));
結果 - >類型錯誤:未定義是不功能
console.log(headers());
結果 - >類型錯誤:未定義是不是一個函數
我採用了棱角分明餅乾(後一個涼亭安裝,在索引腳本標記,注入$餅乾和$超時)也試過:
$timeout(function(){
console.log($cookies.session)
});
結果 - >未定義
另外
console.log($cookieStore.get('Set-Cookie'));
結果 - >未定義。
我已經閱讀了關於這個問題的很多問題,但是沒有一個提出的答案有效。如果有人能指出我朝着正確的方向,我將不勝感激。
我已經試過了。上面的列表並不詳盡,因爲我嘗試了許多變化來尋找正確的方法。 '的console.log(頭(' 設置Cookie '));'在控制檯中顯示爲:'TypeError:undefined不是函數'。那麼這對於這個問題有什麼意義呢?這個錯誤是否暗示了一些潛在的情況? – curchod 2015-02-10 11:22:28
奇怪我剛剛嘗試過使用Plunker,它的工作原理與上述相同。 http://plnkr.co/edit/UThkcCHkottOUQ1R8q43?p=preview – thedoctor 2015-02-10 11:46:03
感謝您與Plunker的鏈接。但是,當我點擊提取時,提示說內容爲空。我使用了Chrome和Firefox。這個問題會是什麼? – curchod 2015-02-10 21:19:46