215

So according to the jQuery Ajax docs, it serializes data in the form of a query string when sending requests, but setting processData:false should allow me to send actual JSON in the body. Unfortunately I'm having a hard time determining first, if this is happening and 2nd what the object looks like that is being sent to the server. All I know is that the server is not parsing what I'm sending.

When using http client to post an object literal {someKey:'someData'}, it works. But when using jQuery with data: {someKey:'someData'}, it fails. Unfortunately when I analyze the request in Safari, it says the message payload is [object Object] ... great... and in Firefox the post is blank...

When logging the body content on the Java side it literally gets [object Object] so how does one send REAL JSON data??

Has anyone had experience with a Java service serializing JSON data in the request body, with the request sent from jQuery?

BTW here is the full $.ajax request:

$.ajax({
    contentType: 'application/json',
    data: {
        "command": "on"
    },
    dataType: 'json',
    success: function(data){
        app.log("device control succeeded");
    },
    error: function(){
        app.log("Device control failed");
    },
    processData: false,
    type: 'POST',
    url: '/devices/{device_id}/control'
});
Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
brad
  • 31,987
  • 28
  • 102
  • 155

2 Answers2

345

An actual JSON request would look like this:

data: '{"command":"on"}',

Where you're sending an actual JSON string. For a more general solution, use JSON.stringify() to serialize an object to JSON, like this:

data: JSON.stringify({ "command": "on" }),

To support older browsers that don't have the JSON object, use json2.js which will add it in.


What's currently happening is since you have processData: false, it's basically sending this: ({"command":"on"}).toString() which is [object Object]...what you see in your request.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • so do I no longer need the processData: false using `JSON.stringify()` ? – brad Nov 11 '10 at 22:19
  • 7
    @brad - correct, it won't matter after doing this because it's *already* a string, and in that case [jQuery won't process it further](https://github.com/jquery/jquery/blob/1.4.3/src/ajax.js#L207). – Nick Craver Nov 11 '10 at 22:23
  • 3
    worked like a charm, many thanks! Is it true then that all request bodies are expected to be strings that get parsed on the other end? – brad Nov 12 '10 at 14:28
  • Why does jQuery parse the `data` into a string, as opposed to just sending it as JSON? – Adam Zerner Jul 15 '17 at 20:57
  • 9
    Side note: if anybody needs it as a "body request payload" instead of form data, don't forget to include `contentType: "application/json; charset=utf-8",` as mentioned in this thread: https://stackoverflow.com/questions/21201270/how-to-submit-json-data-by-request-body-in-jquery – NamedArray Jun 17 '20 at 14:35
0

Seal them up

//contentType: false,
//processData: false,