my dog learned polymorphism*
The moose likes HTML, CSS and JavaScript and the fly likes Submitting form data as JSON with JQuery Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Submitting form data as JSON with JQuery" Watch "Submitting form data as JSON with JQuery" New topic
Author

Submitting form data as JSON with JQuery

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60776
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60776
    
  65

In browsers that support it, JSON.stringify() might be helpful.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Jan 10, 2002
Posts: 60776
    
  65

I like IPAs.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Submitting form data as JSON with JQuery
 
Similar Threads
database best practice
send json object to jsf backing bean
Values from serialize are all null
is it possible to use jquery post in Struts2
Can you post data from textfields to a URL without having a <form>