1

I would like to know how to submit POST data for a form using the jQuery LOAD function, sending the form's serialized data.

so, by way of example, how do i do this:

$('someElement').click(function(){
    var formData = $('formHere').serialize();
    $('anotherElement').load('somePage', formData);
)};

as it stands, this doesn't work - if won't POST the data, but rather sends a GET request. I don't want to start using the $.post method (because i just don't want to!), am i doing something wrong here?

SOLUTION: use serialiazeArray instead - as in

var formData = $('formHere').serializeArray()
bharal
  • 15,461
  • 36
  • 117
  • 195

3 Answers3

8

Easy: use serializeArray() rather than serialize().

serialize() returns a string, whereas serializeArray() returns an object. load() sends a POST if the data is an object. If data is a string, load() sends a GET.

Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
3

According to the jQuery docs for .load() it seems as if the POST method is used if the data (in this case formData is an object. Otherwise it uses GET. However, the docs for .serialize() indicate that it returns a string. Hence always the GET.

  • No idea, but it looks like someone has it covered: http://stackoverflow.com/questions/1184624/convert-form-data-to-js-object-with-jquery –  Jun 17 '12 at 02:54
0

if you must have a post, use $.post otherwise you would need to use $.ajaxSettings to configure global ajax. $.load, $.get() , $.getJSON() & $.post are convenience wrappers of $.ajax with some of the options already taken care of to minimize code required.

 $('someElement').click(function(){
    var formData = $('formHere').serialize();
    $.post('somePage', formData, function(data){
        $(selector).html(data);                                
    });
});

API has examples of using $(form).serialize() with $.post

http://api.jquery.com/jQuery.post/

charlietfl
  • 170,828
  • 13
  • 121
  • 150