将JSON数组传递给Spring MVC Controller
我试图像这样将JSON数组传递到spring MVC Controller中:
var myList = new Array(); data._children.forEach( function (d) {
myList.push( {NAME: d.name, TYPE: d.TYPE, FDATE: d.FDATE } );
});
$.post("/ListRequest", {myList: myList});
控制器如下所示:
@RequestMapping(value="/ListRequest", method = RequestMethod.POST) public void ListRequest(@RequestParam("myList") myList tempmyList )
{
System.out.println(tempmyList);
}
类myList的定义如下:
public class MyList { private List<ListT> ListT;
public List<ListT> getListT() {
return ListT;
}
public void setListT(List<ListT> listT) {
ListT = listT;
}
}
ListT类:
public class ListT { private String NAME;
private String TYPE;
private Long FDATE; ...
我不断收到此错误:HTTP状态400-必需的myList参数“ myList”不存在
还尝试了此请求:
$.ajax({ type: "post",
url: "ListRequest", //your valid url
contentType: "application/json", //this is required for spring 3 - ajax to work (at least for me)
data: JSON.stringify(myList), //json object or array of json objects
success: function(result) {
//do nothing
},
error: function(e){
alert('failure');
}
但出现此错误:JBWEB000120:客户端发送的请求在语法上不正确。
回答:
尝试将其添加到您的ajax调用中,它应该可以修复不受支持的响应:
headers : { 'Accept' : 'application/json',
'Content-Type' : 'application/json'
},
这是对我有用的ajax调用的完整示例:
$.ajax({ dataType : "json",
url : this.baseurl + "/dataList",
headers : {
'Accept' : 'application/json',
'Content-Type' : 'application/json'
},
data : JSON.stringify(params),
type : 'POST',
success : function(data) {
self.displayResults(data);
},
error : function(jqXHR,textStatus,errorThrown ){
showPopupError('Error','error : ' + textStatus, 'ok');
}
});
以上是 将JSON数组传递给Spring MVC Controller 的全部内容, 来源链接: utcz.com/qa/402807.html