Jquery serialize () + custom?
When using $ .POST and $ .GET in jquery, is there a 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 problem is that it is sending the custom code as get, but I want to receive it as a message. Second, I am using it right now, but there is no better way?
+2
a source to share
1 answer
Internally , .serialize()
does a $.param()
on .serializeArray()
so you can do it yourself no matter what you want in between, e.g .:
var obj = $("#form").serializeArray();
obj.CustomVar = 'someValue';
$.ajax({ type:"POST", url:"file.php", data:$.param(obj) });
+8
a source to share