I am trying to get the return value of a AJAX response, and have it all working up to the last step and I'm not sure what I'm doing wrong. I can capture the resulting response, but can't parse it.
AJAX Call:
//Create a response var
var response = '';
//Set up the AJAX Call
function callAjax(Url, data, callBack){
$.ajax({
url: Url,
type: 'POST',
timeout: 10000,
data: data,
success: function(data,textStatus,xhr){
return callBack(xhr);
},
error: function(xhr, status, error){
},
complete: function (data) {
}
});
}
The resulting response looks something like:
{"id":821,"status":true,"server":servername}
I call and try to capture the response with the following
callAjax(<my url>, data, function(myRtrn){
//This shows the proper response if I alert() it
var ajResponse = myRtrn.responseText;
//This Does not work
alert(ajResponse.id);
//This does not work
response = ajResponse;
})
So I can get the proper response into my ajResponse variable, but can't get it outside of the method.
I've seen other examples on StackOverflow, and it seemed to work for them. Basically, I want to the the "id" of the response. What am I missing?
The responseText is a string. If the server responds in a json format, you'll need to parse it before you can use it as an object:
var ajResponse = JSON.parse(myRtrn.responseText);