admin管理员组

文章数量:1326293

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 badges
Add a ment  | 

5 Answers 5

Reset to default 4

contentType: "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