1
這裏之前刪除響應頭的情況是:AngularJs被傳遞給我的控制器
我發了一個帖子請求到服務器上創建一個項目,並希望它返回「201創建」,在新項目的位置位置標題,我可以在其中獲得第二個請求並獲取完整的項目數據。
這些是由服務器發送的響應頭:
Status Code: 201 Created
Connection: keep-alive
Content-Length: 17
Content-Type: application/json
Date: Thu, 22 Sep 2016 08:52:20 GMT
Location: /tasks/4
access-control-allow-headers: Content-type, Cache-Control, Keep-Alive, Location
access-control-allow-methods: GET, POST, PUT, DEL, OPTIONS
access-control-allow-origin: *
access-control-expose-headers: Content-type, Cache-Control, Keep-Alive, Location
然後,當我嘗試從我的控制器得到Location頭,我得到:
// console.log(response.headers)
[content-type,] { content-type="application/json; charset=utf-8"}
在我的Firefox控制檯
,它會拋出一個錯誤,如果我嘗試做:
response.headers['Location']
注意:我使用的是Angular 1.5.8和reco mmended「無極」變種爲$ HTTPS
這裏是我的控制器代碼的摘錄:
app.controller('NewTaskCtrl', function($scope, $http){
$scope.taskName = null
$scope.createTask = function(){
var data=JSON.stringify({ Name: $scope.taskName})
$http.post('http://192.168.1.129:9999/api/tasks',data)
.then(
function(response){
// error Here => console.log('Received Headers', response.headers)
console.log('Received Headers', response.headers())
// Error here => $http.get('http://192.168.1.129:9999'+response.headers['Location'])
$http.get('http://192.168.1.129:9999'+response.headers('Location'))
.then (
function(response){
$scope.Tasks.push(response)
},
function(reason){
alert("Unexpected Error Ocurred:\n"+JSON.stringify(reason))
}
)
},
function(reason){
alert("Unexpected Error Ocurred:\n"+JSON.stringify(reason))
}
)
}
})
爲什麼我失去了頭的任何想法和我如何能得到他們?
編輯。我評論出錯的行,並把正確的在其位,用於記錄
我現在覺得很愚蠢。無論如何,非常感謝您的時間和答案 – mtsdev