I would like to create a function around an ajax call so that I can re-use it. But I'm having trouble populating the data: ({ … }) part of it based on conditions (via a switch).
var d = {};
function lookup (what,etat,compt,vile,zip) {
switch(what) {
case "cou_mun":
d: ({
lookup: "county",
state: etat
})
break;
case "city":
d: ({
lookup: "city",
state: etat,
county: compt
})
break;
case "zipcode":
d: ({
lookup: "zipcode",
state: etat,
county: compt,
city: vile
})
break;
default:break;
}
$.ajax({
url: "socioGeo_envnLookup.php",
type: "POST",
data: d,
//lookup: "county", //"county" or "city" or "zipcode"
//state: etat
//county: cou_mun,
//city: city,
success: function(what,data) {
$('select[name="'+what+'"]').html(data);
}
}); //$.ajax
} //lookup()
And then I would call it like lookup("city",state,county);
IE complains when I try to make an ajax call that sends undefined variables, so I want to set data: ({ … }) dynamically.
dwhich is done by the assignment operator=. Also make sure that you definedlocally in your function.whatinsuccess:as the value that should be indata? I didalert('what = '+what+'\n'+'data = '+data.toSource);inside of success andwhat= the html that should be indataanddata = function toSource() { [native code] }