I’m creating a JSON object and pushing it into an array.
var json_array = []
var edit_info = {
'name': str_name,
'id': str_id,
};
json_array.push(JSON.stringify(edit_info))
and trying to send the json_array
in a POST request.
$.ajax({
dataType: 'JSON',
url: req_url,
type: req_method,
data: {'req_data': json_array},
success: function(data){
console.log(data.message)
},
error: function(){
alert("Some error has occured.")
}
});
I am seeing strange behaviour from the browser (Chrome): it seems it passes two arrays as the post data:
req_data[]:{"name":"naveen","id":"11"}
req_data[]:{"name":"kavi","id":"13"}
Shouldn't it be like this?
req_data[]: [{"name":"naveen","id":"11"}', {"name":"kavi","id":"13"}]
But in views.py request.POST
shows post data as it should be:
{u'req_data[]': [u'{"name":"naveen","id":"11"}', u'{"name":"kavi","id":"13"}']}
But request.POST['req_data[]']
shows
{"name":"kavi","id":"13"}
I have no clue what is happening here. Anyone?
You need to use QueryDict.getlist method for request.POST and then deserialize each list item from str to dict:
import json
data = request.POST.getlist('req_data[]', [])
data = [json.loads(item) for item in data]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句