AngularJS $ http发布对象包含数组奇怪的行为

时间:2014-08-28 03:39:11

标签: javascript python json angularjs

我有一个使用$ http发布params的函数。我不明白的是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')])

1 个答案:

答案 0 :(得分:0)

我很久以前也有这个问题,看来这是角度$ 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数据。