I have a HTML form with several text input fields. The submitted data will be sent to a backend system with AJAX / JSON. In jQuery, I do this, which works fine:
$("#addPerson").submit(function() {
var person = $(this).serializeObject();
$.postJSON("person/add/", person, function(data) {
...
});
});
Now I have the problem, that I do not have a form, but I also want to add a person.
I have the values: firstname = Tim
, lastname = Smith
, address = a street with number
, ...
But how can I create the key-value pairs, so I have the same like the line var person = $(this).serializeObject();
submitted by a form and I can make a ca开发者_StackOverflow社区ll with $.postJSON
?
Thank you in advance & Best Regards, Tim.
The json object would look like this... I think this is what you're looking for.
var first,
last,
addr;
var jsonObj = {
firstname: first,
lastname: last,
address: addr
}
You are looking for the jQuery param function: http://api.jquery.com/jQuery.param/
$.param(object);
or the serialize function: http://api.jquery.com/serialize/
$(jQuery_Collection).serialze()
But, I believe you can simply pass the object instead of the string, and jQuery will do it for you.
精彩评论