javascript JQuery ajax/post - 在数据字段中设置 JSON 会导致 JsonMappingException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9103771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery ajax/post - setting JSON in the data field causes JsonMappingException
提问by Travis Nelson
I am having issues sending a JQuery post to my server. I have narrowed the problem down to the data field not being set correctly in my javascript, which leads to an exception:
我在向我的服务器发送 JQuery 帖子时遇到问题。我已将问题缩小到我的 javascript 中未正确设置数据字段,这导致了异常:
org.codehaus.Hymanson.map.JsonMappingException: Can not deserialize instance of com.myserver.rest.messages.dto.NewMessageDTO out of START_ARRAY token
org.codehaus.Hymanson.map.JsonMappingException:无法从 START_ARRAY 令牌中反序列化 com.myserver.rest.messages.dto.NewMessageDTO 的实例
The code that works, but not very clean:
该代码的工作,但不是很干净:
$.ajax({
type: "POST",
url: "../resources/messages/",
data: '{"subject": "' + subject +
'", "message": "' + message +
'", "messageType": "' + type +
'", "employeeIDs": [' + employeeIDs +
'], "assignmentIDs": [' + assignmentIDs +']}',
contentType: "application/json",
success: successHandler,
error: defaultErrorHandler
});
Which renders to:
这呈现为:
{"subject": "test", "message": "test", "messageType": "MESSAGE", "employeeIDs": [461,485], "assignmentIDs": [103]}
{"subject": "test", "message": "test", "messageType": "MESSAGE", "employeeIDs": [461,485], "assignmentIDs": [103]}
The code that does not work, but is cleaner:
不起作用但更干净的代码:
$.ajax({
type: "POST",
url: "../resources/messages/",
data: {'subject': subject, 'message': message, 'messageType': type, 'employeeIDs[]': employeeIDs, 'assignmentIDs[]': assignmentIDs},
contentType: "application/json",
success: successHandler,
error: defaultErrorHandler
});
Which is rendering as application/x-www-form-urlencoded for some reason:
由于某种原因呈现为 application/x-www-form-urlencoded :
subject=test&message=test&messageType=MESSAGE&employeeIDs%5B%5D=461&employeeIDs%5B%5D=485&assignmentIDs%5B%5D=103
subject=test&message=test&messageType=MESSAGE&employeeIDs%5B%5D=461&employeeIDs%5B%5D=485&assignmentIDs%5B%5D=103
Any ideas what I am doing wrong here? Thanks in advance for any help
任何想法我在这里做错了什么?在此先感谢您的帮助
回答by pete
contentType: "application/json"
means you are expecting JSON as a result of the call. All data is converted to a query stringprior to sending.
contentType: "application/json"
意味着您期待 JSON 作为调用的结果。在发送之前,所有数据都被转换为查询字符串。
The less-clean code is sending the data as a JSON string, which is apparently what ../resources/messages/
expects.
不太干净的代码将数据作为 JSON 字符串发送,这显然是我们所../resources/messages/
期望的。
To make that more clean and keep the data as a JSON string:
为了使其更干净并将数据保留为 JSON 字符串:
$.ajax({
'type': 'POST',
'url': '../resources/messages/',
'data': '{' +
"'subject':" + subject + ','
"'message':" message + ','
"'messageType':" type + ','
"'employeeIDs':[" employeeIDs + '],'
"'assignmentIDs':[" assignmentIDs + ']' +
'}',
'contentType': "application/json",
'success': successHandler,
'error': defaultErrorHandler
});
or
或者
var data = {
'subject': subject,
'message': message,
'messageType': type,
'employeeIDs': employeeIDs[]
'assignmentIDs': assignmentIDs[]
};
$.ajax({
'type': 'POST',
'url': '../resources/messages/',
'data': JSON.stringify(data), //assuming you have the JSON library linked.
'contentType': "application/json",
'success': successHandler,
'error': defaultErrorHandler
});
Hope this helps,
希望这可以帮助,
Pete
皮特
回答by ShankarSangoli
Try this, it should work fine and much cleaner.
试试这个,它应该可以正常工作并且更干净。
$.ajax({
type: "POST",
url: "../resources/messages/",
data: {
subject: subject,
message: message,
messageType: type,
employeeIDs: employeeIDs,
assignmentIDs: assignmentIDs
},
contentType: "application/json",
success: successHandler,
error: defaultErrorHandler
});
The issue in your first part of the code is, your are sending the whole data as one string where it should be a key/value pair for each data attribute. In the second part you are not creating the key names properly for array fields. You don't have to include []
for array fields.
代码第一部分的问题是,您将整个数据作为一个字符串发送,其中每个数据属性应该是键/值对。在第二部分中,您没有为数组字段正确创建键名。您不必包含[]
数组字段。
回答by Jasper
The arrays are a bit wacky, try this:
数组有点古怪,试试这个:
{'subject': subject, 'message': message, 'messageType': type, 'employeeIDs': employeeIDs, 'assignmentIDs': assignmentIDs}
This assumes that employeeIDs
and assignmentIDs
are arrays.
这假设employeeIDs
和assignmentIDs
是数组。
Here is a demo: http://jsfiddle.net/jb6MA/
这是一个演示:http: //jsfiddle.net/jb6MA/
回答by seth.miller
In your working example you have variables called employeeIDs
and assignmentIDs
but in your non-working code, you have appended []
to the end of each.
在您的工作示例中,您调用了变量employeeIDs
,assignmentIDs
但在您的非工作代码中,您已附加[]
到每个变量的末尾。
回答by Amit
In the JSON schema, strings are only valid if they are enclosed with double-quotes: http://www.json.org/. A lot of implementations are not strict about this, but it looks like your server's implementation is.
在 JSON 模式中,字符串仅在用双引号括起来时才有效:http: //www.json.org/。许多实现对此并不严格,但看起来您的服务器的实现是。