4

How can I add parameters to a post request using .post or .ajax. I tried:

var formData = $('form').serialize();
$.ajax({
url: "url",
data: formData,
type: "POST",
dataType: "dataType"
});

but that doesn't seem to work.

SimplGy
  • 20,079
  • 15
  • 107
  • 144
TrashCan
  • 817
  • 4
  • 13
  • 20
  • Have you looked at this, previous, answer on SO? http://stackoverflow.com/questions/370359/passing-parameters-to-a-jquery-function – Hampus Jun 07 '12 at 13:47
  • Yes I did, I was a little bit frustrated. I tried this alot.. I dont get why when I do the following, `code`$(document).ready(function() { $('searchsubmit').click(function() { var formData = $('form').param(); $.ajax({ url: 'Search', type: "POST", data: formData, succes: function(data) { alert(data); } }); alert("blabla"); }); alert("blabla"); });`code` My servlet does nothing. – TrashCan Jun 07 '12 at 14:09

5 Answers5

3

Just add it to your formData before send?

var formData = $('form').serialize();
$.ajax({
    url: "url",
    data: formData + '&param=' + param_value,
    type: "POST",
    dataType: "dataTpe"
});
Sergei Zahharenko
  • 1,514
  • 12
  • 16
1
$.ajax({
    url: "url",
    type: "POST",
    //dataType: 'json',
    data: formData,   
    success: function(data){

    }
});
Thulasiram
  • 8,432
  • 8
  • 46
  • 54
0

Use .param, not .serialize. The former returns an object, the latter returns a string.

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

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
0
data: $('#myForm').serialize() + "&moredata=" + morevalue
swapnesh
  • 26,318
  • 22
  • 94
  • 126
0

following acrashik's answer the following code works:

     var aData = table.fnGetData( this,0 );
     $.ajax({
         url: "MessageDetail",
         type: "POST",
         data: "messageid=" + aData,
         succes: function(data) {
            alert(data);
        }
     });
TrashCan
  • 817
  • 4
  • 13
  • 20