This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Submitting form data as JSON with JQuery

 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought if I submit with $.post() in JQuery that the data on the form will be sent to the server in JSON. I even tried $.ajaxSetup({contentType: "application/json"});

However, when I look at the String in the RequestBody it isn't in JSON. It shows like

login=bytor&password=password&firstName=mark&lastName=spritzler

So the data is like a RequestParam, not a RequestBody.

I am able to generate a Java domain object when the object has all those fields, but for registration like above, I don't have them fill out all the fields in the Java domain object, so when the serverside receives it, It doesn't know what to do.

I also tried some code I found to make sure the string would be json, but it still looks exactly like above



Here is my submit code



Thanks for your help.

Mark
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure why you thought that the body would be JSON. POSTs are usually encoded as x-www-form-urlencoded.

You can format the JSON yourself, and use the data and processData options to pass the JSON as the body.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In browsers that support it, JSON.stringify() might be helpful.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

Basically, I have a serverside controller using Spring MVC, and it is simple to convert JSON to Java domain object where you annotate the parameter with @RequestBody, and since Jackson jar is in my classpath, it would automatically convert the json in the body to my domain object.

While not in the body I am able to have Spring MVC automatically convert to one of my domain object because all the params are set to all the properties in the domain object. But for others where I send partial data compared to what the domain object stores, I get failure to even call the method.

Basically, my Spring MVC Controllers are just all REST apis, and that is what I want. Since I am making this a single page web app.

Thanks again for all your help Bear. Next beer when we see each other again is on me.

Mark
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like IPAs.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sweet, I found I can put @ModelAttribute on my parameter in my controller and it will automatically take the form post data and for each field find a field in my Domain object with matching name and populate it.

So after weeks, I finally got my registration to post the data to my Controller, and my controller saving it to my Neo4j Graph database. Then did a get to get all users and I see my new user in there.

Thanks again. I keep forgetting that the browsers don't really support REST in forms to allow PUTs or DELETES.

See my main goal is after first loading the index page, everything is now down to the browser and all the rest of the communication is passing data only, no html anymore for the single page app to work.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic