admin管理员组文章数量:1326474
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:
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of .myserver.rest.messages.dto.NewMessageDTO out of START_ARRAY token
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]}
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:
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
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:
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of .myserver.rest.messages.dto.NewMessageDTO out of START_ARRAY token
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]}
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:
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
Share Improve this question asked Feb 1, 2012 at 21:41 Travis NelsonTravis Nelson 2,6205 gold badges29 silver badges34 bronze badges5 Answers
Reset to default 4contentType: "application/json"
means you are expecting JSON as a result of the call. All data is converted to a query string prior to sending.
The less-clean code is sending the data as a JSON string, which is apparently what ../resources/messages/
expects.
To make that more clean and keep the data as a JSON string:
$.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
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.
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.
Here is a demo: http://jsfiddle/jb6MA/
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.
In the JSON schema, strings are only valid if they are enclosed with double-quotes: http://www.json/. A lot of implementations are not strict about this, but it looks like your server's implementation is.
本文标签: javascriptJQuery ajaxpostsetting JSON in the data field causes JsonMappingExceptionStack Overflow
版权声明:本文标题:javascript - JQuery ajaxpost - setting JSON in the data field causes JsonMappingException - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742209629a2433467.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论