I'm trying to create a function which uses the jQuery function getJSON but I'm running in to a problem. The first part of the callback can't be variable, jQuery always interprets it as a string.
My code:
$(document).ready(function () {
function getName(callbackName, callbackVal){
$.getJSON("json_server.php",{callbackName:callbackVal}, function(result){
//Do stuff
});
}
getName("name", "john");
});
Which results in the following request URL:
".../json_server.php?callbackName=john"
instead of
".../json_server.php?name=john"
I already tried escaping it but that only results in errors.
What am I doing wrong, any suggestions?
You could just pass an object:
function getName(data){
$.getJSON("json_server.php",data, function(result){
//Do stuff
});
}
getName({"name": "john"});
Or build it in the function like this:
function getName(callbackName, callbackVal){
var data = {};
data[callbackName] = callbackVal;
$.getJSON("json_server.php",data, function(result){
//Do stuff
});
}
getName("name", "john");