leora
leora

Reputation: 196489

What is the best way to add extra data to a jquery ajax post (in addition to a serialized form)?

I have the following code:

 $.post('/Calendar/Add', $("#calendarForm").serialize(), function (data) {

 });

and i want to add another piece of data in addition to all of the items in the #calendarForm.

so lets say i want to append an additional key value pair to the querystring:

  personId=223

I would rather not add extra hidden inputs into the calendarForm as that is what i am doing now and its kind of messy. Is there any easy way to add an additional piece of data besides all of the value in the calendarForm when i call this post ?

I tried something like this:

 $.post('/Calendar/Add', $("#calendarForm").serialize() + "&personId=223", function (data) {

 });

but that didn't seem to work?

Upvotes: 1

Views: 1426

Answers (2)

Bhushan
Bhushan

Reputation: 6181

Try using serializeArray

var data = $('#myFormName').serializeArray();
data.push({name: 'myParamName', value: 'MyParamValue'});

Update1:

You can use following code in $.post:

$.post('/Calendar/Add', data, function (data)    
{});

For more information have look at this.

Upvotes: 2

Jyoti Prakash
Jyoti Prakash

Reputation: 1260

you can do it like bellow

$.post('/Calendar/Add', { personId: 223,calendarForm: $("#calendarForm").serialize() },
         function (data) { });

Upvotes: 0

Related Questions