[英]AngularJS $http posting object containing array strange behaviour
我有一個使用$ http發布參數的函數。 我不明白的是, filters
參數包含一個數組。 但是,服務器將此數組解釋為該數組中每個項目的鍵/值對。 為什么? 這是AngularJS還是服務器端問題?
var updateCandidates = function (slot) {
console.log(slot.filters)
$http({method: 'POST', url: '/api/slot_candidates', params: {
type: slot.type,
start_date: slot.start_date,
end_date: slot.end_date,
filters: slot.filters
}}).success(function (response) {
return response
}).error(function (response) {
$rootScope.modalAlert('error', 'Something happened', true)
})
}
# request.params. Note the duplicate 'filters' key
NestedMultiDict([(u'end_date', u'2014-12-30T14:00:00'), (u'filters', u'{"operator":"contains","group":"program","type":"unicode","name":"title","query":"joan of arc"}'), (u'filters', u'{"operator":"contains","group":"program","type":"unicode","name":"aspect_ratio","query":"16"}'), (u'start_date', u'2014-08-25T00:00:00'), (u'type', u'Program')])
我在很久以前就有這個問題,似乎這是有角度的$ http發布的行為,您不能直接使用數組發布參數,我認為這是用JSON發布它們的好方法,
var updateCandidates = function (slot) {
console.log(slot.filters)
var data = {
type: slot.type,
start_date: slot.start_date,
end_date: slot.end_date,
filters: slot.filters
};
$http({
method: 'POST',
url: '/api/slot_candidates',
responseType : "json",
params: JSON.stringify(data),
headers:{
'Content-Type':'application/json'
},
}).success(function (response) {
return response
}).error(function (response) {
$rootScope.modalAlert('error', 'Something happened', true)
})
}
但請確保您的服務器也支持json數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.