-------------------ajax传递两个对象------------------------------
var scatalog = new Object();
scatalog.id =3;
scatalog.pId = 5;
var scontent = new Object();
scontent.id=6;
scontent.catalogId =66;
$.ajax({
type : 'POST',
url : 'testSendValue/sendValue',
contentType : 'application/json;charset=utf-8', //设置请求头信息
dataType:"json",
data :JSON.stringify({
//下面的左方的scatalog和scontent名字必须和TestSendValueDto 类的这两个类的属性名保持一致!!!!
scatalog:scatalog,
scontent:scontent
}),
success: function(data){
alert(data);
}
});
---------------------后台--------------------------
@RequestMapping("sendValue")
@ResponseBody
public String test(@RequestBody TestSendValueDto dto){
return "succeed";
}
------------------------------------------------
public class TestSendValueDto {
public StandardCatalog scatalog;
public StandardContent scontent;
public StandardCatalog getScatalog() {
return scatalog;
}
public void setScatalog(StandardCatalog scatalog) {
this.scatalog = scatalog;
}
public StandardContent getScontent() {
return scontent;
}
public void setScontent(StandardContent scontent) {
this.scontent = scontent;
}
-----------------------ajax传递数组--------------------------
// 传数组
// 切记,如果你的array没有push任何东西,只是new了一下,
// 这样是无法进到后台的
// 如果值为空,那么传值之前一定要加上array=[null];!!!!
// 否则进不了后台!!!!!
var array = new Array();
array.push(1);
array.push(2);
$.post("testSendValue/sendValue",
{array:array,id:3}
,function(data){
alert(data);
});
----------------后台接收--@RequestParam里的名字("array[]")必须和你传的一样-------------------------
@RequestMapping("sendValue")
@ResponseBody
public String test(@RequestParam("array[]")List<String> array1,int id){
return "succeed";
}
ajax传递两个对象和传数组
最新推荐文章于 2021-08-06 14:03:27 发布