2014-08-28 40 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数据。