I am very new to JavaScript, so the question may be fictitious. I make a lot of GET and POST requests in my code, so I decided to write a wrapper function to call the jQuery ajax function
function getRequest(url,successCallback,failCallback){
$.ajax({
url: url,
timeout: 3000,
dataType: "json",
success: function(data,textStatus,jqXHR){
successCallback(data,textStatus,jqXHR);
},
error: failCallback(jqXHR,textStatus,errorThrown)
});
}
I call it this way:
url = "/api/info";
function onSuccess(data,textStatus,jqXHR){
$("#result").html("Hostname is "+data.hostname);
}
function onFail(jqXHR,textStatus,errorThrown){
$("#result").html("Shit happens");
}
getRequest(url,onSuccess(),onFail());
The callback starts, but the data is undefined.
source
share