Monday, November 2, 2009

JQuery - ajax call with dynamic parameters

This one was discovered because a colleague of mine asked me, and I started to wonder and realized I never used it before, never really needed it..But indeed, it can be quite handy!
Before you make the $.ajax call or $.post call or whatever, you do this:

var parameters = {}; //declare the object
parameters["prop1"] = "value1"; //set some value
parameters["prop2"] = "value2"; //and again

Then, when you're making the actual request, you pass your object on the data field

type: "POST",
url: "some.php",
data: parameters,
success: function(msg){

and voilá! you can build your parameters in a nice way, instead of "prop1=value1&prop2=value2"

Be back soon :)

No comments: