[英]$http - Headers not returning - AngularJS
我正在嘗試設置Angular Service,以從將JSON文件提供給應用程序的節點服務器中檢索數據。 我檢查了節點服務器的URL,它們都輸出了應有的JSON,但是由於HTTP標頭,Angular似乎無法獲取它們。 從一些谷歌搜索來看,這似乎是一個常見問題,我不確定是否可以解決,甚至可以解決嗎?
eventsApp.factory('eventData', function($http, $log) {
return {
getEvent: function(successcb) {
$http({method: 'GET', url: 'http://localhost:8000/data/event/1'}).
success(function(data, status, headers, config){
successcb(data);
}).
catch(function(data, status, headers, config){
$log.warn(data, status, headers, config);
})
}
}
});
在控制台中,我從警告中得到以下警告:
對象{數據:空,狀態:0,標題:headersGetter / <(),配置:對象,statusText:“”}未定義未定義未定義
從文檔中
// Simple GET request example:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
響應對象具有以下屬性:
- data –
{string|Object}
–使用轉換函數轉換的響應主體。- status –
{number}
–響應的HTTP狀態代碼。- 標頭–
{function([headerName])}
–標頭獲取函數。- config –
{Object}
–用於生成請求的配置對象。- statusText –
{string}
–響應的HTTP狀態文本。
因此,您應該能夠使用以下方法獲取感興趣的標頭:
response.headers('header-name')
根據您的響應日志記錄result.data
: result.data
為null。 因此,您應該檢查Chrome Dev Tools
的“ Network
標簽,並檢查服務器發出的HTTP響應。
status
0表示可能存在CORS問題。 因此,如果您的角度應用程序托管在您嘗試向其發送http請求的服務器之外的其他主機或端口上(在本例中為localhost:8000),則應將SERVER配置為允許CORS請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.