AJAX 发布列表不起作用
AJAX posting a list doesnt work
我正在尝试 post 一组名称到 Web 服务。下面是我在JQueryAJAX中写的代码:
var poster=[1,2,3]
$.ajax({
type: "POST",
traditional:true,
url:"/post",
data: {'poster':poster},
dataType: 'JSON',
cache: false,
success: function(result){console.log(result);}
});
结果是,只有“3”(数组中的最后一个元素)被发布。我的console.log也returnsObject{poster:"3"}
。我尝试了一切,从添加传统关键字到使数据匿名,如 data: {'':poster} 等等。没有任何效果。有人可以帮忙吗?
jQuery 将它们变成三个 POST
个参数,例如 poster[]=1&poster[]=2&poster[]=3
。您应该在服务器端收到三个 poster[]
参数。第一个是 1,第二个是 2,第三个是 3。我猜你只取回最后一个,所以你只得到 3。你需要的是得到所有这些。
尝试使用 JSON.stringify()
var dataToBePosted = { poster: [1, 2, 3] };
$.ajax({
type: "POST",
url:"/post",
data: JSON.stringify(dataToBePosted),
dataType: 'JSON',
cache: false,
success: function(result){console.log(result);}
});
像这样尝试
var poster=[1,2,3];
$.ajax({
type: "POST",
traditional:true,
url:"/post",
data: {'poster':JSON.stringify(poster)},
dataType: 'JSON',
cache: false,
success: function(data){
console.log(data);
}
});
或
var poster=[1,2,3];
$.ajax({
type: "POST",
traditional:true,
url:"/post",
data: 'poster='+JSON.stringify(poster),
dataType: 'JSON',
cache: false,
success: function(data){
console.log(data);
}
});
我正在尝试 post 一组名称到 Web 服务。下面是我在JQueryAJAX中写的代码:
var poster=[1,2,3]
$.ajax({
type: "POST",
traditional:true,
url:"/post",
data: {'poster':poster},
dataType: 'JSON',
cache: false,
success: function(result){console.log(result);}
});
结果是,只有“3”(数组中的最后一个元素)被发布。我的console.log也returnsObject{poster:"3"}
。我尝试了一切,从添加传统关键字到使数据匿名,如 data: {'':poster} 等等。没有任何效果。有人可以帮忙吗?
jQuery 将它们变成三个 POST
个参数,例如 poster[]=1&poster[]=2&poster[]=3
。您应该在服务器端收到三个 poster[]
参数。第一个是 1,第二个是 2,第三个是 3。我猜你只取回最后一个,所以你只得到 3。你需要的是得到所有这些。
尝试使用 JSON.stringify()
var dataToBePosted = { poster: [1, 2, 3] };
$.ajax({
type: "POST",
url:"/post",
data: JSON.stringify(dataToBePosted),
dataType: 'JSON',
cache: false,
success: function(result){console.log(result);}
});
像这样尝试
var poster=[1,2,3];
$.ajax({
type: "POST",
traditional:true,
url:"/post",
data: {'poster':JSON.stringify(poster)},
dataType: 'JSON',
cache: false,
success: function(data){
console.log(data);
}
});
或
var poster=[1,2,3];
$.ajax({
type: "POST",
traditional:true,
url:"/post",
data: 'poster='+JSON.stringify(poster),
dataType: 'JSON',
cache: false,
success: function(data){
console.log(data);
}
});