Note that there are some explanatory texts on larger screens.

plurals
  1. POPosting JSON via JQuery to a Spring Roo Controller
    primarykey
    data
    text
    <p>I recently ran into an issue using JQuery and Spring Roo. I wanted to post data to a JSON enabled URL of a Spring Roo controller (I don't think this is especially important, but I put it out there just for clarity's sake). I knew that the controller worked fine because I could post data to it via Curl command, like this:</p> <pre><code>curl -i -X POST -H "Content-Type:application/json" http://localhost:8080/PeopleProject/people -d '{"firstName":"some","lastName":"guy","email":"some@guy.com"} </code></pre> <p>So, then I tried posting data from a $.post() call in Javascript, like this:</p> <pre><code> var data = {"firstName":"some","lastName":"guy","email":"some.guy@gmail.com"}; $.post('/PeopleProject/people', data, successfulPost ) ; </code></pre> <p>However, I would get errors on the server side about not being able to parse the input. Running the debugger, I could see that the data being posted via this Javascript was coming across URL encoded, whereas the data from the curl command was not.</p> <p>Finally, after much messing around, I tried this Javascript:</p> <pre><code> var jqxhr = $.ajax({ url: "/PeopleProject/people", processData : true, contentType:"application/json", type : "POST", data: '{"firstName":"some","lastName":"guy","email":"some.guy@gmail.com"}', }) </code></pre> <p>And this worked! But, I am not sure why it worked and the $.post() did not. Can anyone more knowledgeable than me about Javascript/JQuery explain this?</p> <p>I thought I read somewhere that a JSON post to a server would encode stuff as name value pairs, but I am guessing this isn't right since I have seen that a the curl command posted nice, clean JSON, while the $.post() pushed a URL encoded query string.</p> <p>So my question is: is the above ajax() call the preferred method for pushing JSON data to a server? Or, is there a way to use $.push() and get it work?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload