2014-08-28 74 views
0

我有一個函數使用$ http發佈參數。我不明白的是filters param包含一個數組。但是,服務器將此數組解釋爲數組中每個項的鍵/值對。爲什麼?這是一個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')]) 

回答

0

我有這個問題太很久以前,好像這是角$ HTTP POST的行爲,你不能陣列之後的參數直接,我認爲這是使用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數據。