我看网上很多人都这样写 但我反复试了几次 死活不行 麻烦大家帮忙看看
我的是spring mvc3.1.3
代码如下
java
@RequestMapping(value = "testRequestBody.do", method = RequestMethod.POST)
public void testRequestBody(@RequestBody List<UserVO> users) throws UnsupportedEncodingException {
System.out.println("in here");
System.out.println(users.size());
System.out.println("end");
}
用户对象类
public class UserVO {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public UserVO() {
}
public UserVO(String name, String pwd) {
this.name = name;
this.pwd = pwd;
}
}
javascript
var users = JSON.stringify([
{name: "user1", pwd: "123"},
{name: "user2", pwd: "123"}
]);
alert(users);
$.ajax({
type: "post", // 请求方式
url: "testRequestBody.do", //url地址
data:users, //数据
contentType: "application/json",
dataType: "json",
success: function (response, ifo) {
alert("success");
}, error: function () {
alert("error");
}
})
chrome 后台错误 Failed to load resource: the server responded with a status of 415 (Unsupported Media Type)
问题补充:@RequestBody List<UserVO> users 如果将这里去掉
则可以成功访问到方法 说明ajax是能访问服务器的
只是在转换对象的时候出错了 但应该怎么写呢?