views:

35

answers:

1

When using $.POST and $.GET in jquery is there any way to add custom vars to the URL and send them too? i tried the following :

$.ajax({type:"POST", url:"file.php?CustomVar=data", data:$("#form").serialize()});

And :

<input name="CustomVar" type="hidden" value="data" />
$.ajax({type:"POST", url:"file.php", data:$("#form").serialize()});

The first ones problem is that it send the custom as get but i want to receive it as post. The second one well i'm using it right now but there is not any better way?

+2  A: 

Internally, .serialize() does a $.param() on .serializeArray() so you can do that yourself adding whatever you want in-between, like this:

var obj = $("#form").serializeArray();
obj.CustomVar = 'someValue';
$.ajax({ type:"POST", url:"file.php", data:$.param(obj) });
Nick Craver