How to add data via $.ajax ( serialize() + extra data ) like this

Jquery

Jquery Problem Overview


I want to add extra data after i use $('#myForm').serialize() + extra data

$.ajax({
   type: 'POST',
   url: $('#myForm').attr('action'),
   data: $('#myForm').serialize(),   // I WANT TO ADD EXTRA DATA + SERIALIZE DATA
   success: function(data){
      alert(data);
      $('.tampil_vr').text(data);
   }
});

Jquery Solutions


Solution 1 - Jquery

What kind of data?

data: $('#myForm').serialize() + "&moredata=" + morevalue

The "data" parameter is just a URL encoded string. You can append to it however you like. See the API here.

Solution 2 - Jquery

Personally, I'd append the element to the form instead of hacking the serialized data, e.g.

moredata = 'your custom data here';

// do what you like with the input
$input = $('<input type="text" name="moredata"/>').val(morevalue);

// append to the form
$('#myForm').append($input);

// then..
data: $('#myForm').serialize()

That way, you don't have to worry about ? or &

Solution 3 - Jquery

You can do it like this:

postData[postData.length] = { name: "variable_name", value: variable_value };

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoko WandiroView Question on Stackoverflow
Solution 1 - JqueryjthompsonView Answer on Stackoverflow
Solution 2 - JquerysteadwebView Answer on Stackoverflow
Solution 3 - JqueryPrakashGView Answer on Stackoverflow