我将获得的数据通过ajax 发送到Python后台,如果发送的是数组且直接将数组发到后台,后台无法解析。解决办法是,先将数组序列化,及使用JSON.stringify()将数组序列化为字符串然后发给后台,后台就可以解析出来。而如果后台将数据序列化后发给我,我需要调用JSON.parse()将字符串解析为Javascript值,然后我获取值。
function deleteFile(){ //删除
var allitems = $('tbody tr:not(.firstTr):has(:checked)');
var itemsLength = allitems.length;
if(itemsLength > 0) {
var fileIdArray = new Array();
for(var i=0; i<itemsLength; i++){ //可批量删除
fileIdArray.push($(allitems[i]).attr('id'));
}
sendData(fileIdArray);
hideDialog();
}
}
function sendData(fileIdArray){
$.ajax({
type:"post",
dataType:"json",
contentType:"application/x-www-form-urlencoded;charset=UTF-8",
url:"{% url netPan.File.views.delFile%}",
data:{
ids: JSON.stringify(fileIdArray) //将数组序列化为字符串
},
success:function(data){
var message = data.message;
if(message == 'F'){
alert('deleteFile success');
}else if(message == 'G'){
alert('deleteFile fail');
}
},
error: function(xhr,textStatus,errorThrown){
}
});
}
function updateLists(){ //更新列表数据
$.ajax({
type:"post",
dataType:"json",
contentType:"application/x-www-form-urlencoded;charset=UTF-8",
url:"{% url netPan.File.views.myFile%}",
data:{},
success:function(data){
var allFiles = JSON.parse(data.myfiles); //解析为Javascript值
if(allFiles.length != 0){
showLists(allFiles);
}else{
$('tbody').html('无数据记录');
}
},
error: function(xhr,textStatus,errorThrown){
}
});