【发布时间】:2014-08-28 10:04:50
【问题描述】:
我在发送和接收 json 对象列表 (jQuery Ajax) 时遇到问题
Java 对象
public class UserSkill () {
private long user;
private long skill;
private long level;
//getter and setter methods
}
从控制器我得到对象列表,它看起来像这样
$.getJSON("getList", function(data) {
console.log(data);
});
//console log -> [Object { user=4, skill=61, leve=10}, Object { user=3, skill=61, level=20}]
我更改了一些值,我们有以下代码
ioarray = [];
//update methods
console.log(ioarray);
//console log -> [Object { user=4, skill=61, level=9000 }, Object { user=3, skill=61, level=100 }]
Ajax POST
$.ajax({
url : 'goUpdate',
type : 'POST',
contentType : 'application/json; charset=utf-8',
dataType : 'json',
data: ioarray,
succcess : function(e) {
alert('Yeah');
}
控制器
@RequestMapping(value = "goUpdate", method = RequestMethod.POST)
public Object goUpdatePOST(@RequestBody List<UserSkill> list) {
//list.get(0).getLevel();
return list;
}
日志
type Status report
message Request method 'POST' not supported
description The specified HTTP method is not allowed for the requested resource.
这里出了点问题...有什么想法吗?
更新;
控制器
@RequestMapping(value = "goUpdate", method = RequestMethod.POST)
public @ResponseBody String goUpdatePOST(@RequestBody UserSkill[] list) {
for(UserSkill i : list) {
System.out.println(i.getSkill());
}
return "somepage";
}
jQuery
var ioarray = [{ user:4, skill:61, level:10},
{ user:3, skill:61, level:20}];
$.ajax({
url : 'goUpdate',
type : 'POST',
data: JSON.stringify(ioarray),
});
控制台输出
JSON
0
Object { user=4, skill=61, level=10}
1
Object { user=3, skill=61, level=20}
Source
[{"user":4,"skill":61,"level":10},{"user":3,"skill":61,"level":20}]
在 pom.xml 中插入了 jackson-mapper-asl 和 jackson-core-asl。
当然这个例子会产生同样的错误......我做错了什么?我想我检查了所有可能性。
【问题讨论】:
-
尝试删除
dataType : 'json' -
否... POST 来源:undefined=&undefined=
-
尝试删除
@RequestBody然后,发布的数据及其类型存在一些问题。 -
我确实喜欢在数据中:{list: ioarray},仍然像上面一样出现,但现在发布:list%5B0%5D%5Buser%5D=4&list%5B0%5D%5Bskill%5D= 61&list%5B0%5D%5Blevel%5D=10&list%5B1%5D%5Buser%5D=3&list%5B1%5D%5Bskill%5D=61&list%5B1%5D%5Blevel%5D=20
-
所以现在您的数据正在使用 UT-8 格式进行编码,请尝试删除
dataType : 'json'