Jacob Straszynski
Jacob Straszynski

Reputation:

jQuery.post() Using serialized form data, and custom object in submission

When doing ajax stuff with jQuery you can pass your data along to the server in two key ways:

  1. Use an object like

    {
     firstname:'blah',
     lastname: 'derp'
    }
    
  2. Use a string like &firstname=blah&lastname=derp

(1) Arises naturally when you're passing in values programmatically.

(2) Arises naturally when you've got input fields.

My problem: it doesn't look like you can combine them, i.e.

jQuery.extend({
 firstname:'blah',
 lastname:'derp'
},jQuery('form.some-form').serialize());

And it's been nagging me for a while.

Truthfully I think they just need an objectified version of the serialize method that just gives you a json representation.

Upvotes: 4

Views: 6263

Answers (1)

karim79
karim79

Reputation: 342635

This previous discussion should solve the problem of converting from json to a query string, and vice-versa:

How can I convert query string or JSON object map to single JSON object with jQuery?

Also, you might want to have a look at the jQuery Form plugin

Upvotes: 1

Related Questions