erichfosse
IS-IT--Management
I've been experimenting with jQuery.ajax(), and I have come to the conclusion that everything in the scope of the .done()-function in the ajax-call is contained within the scope of the .done()-function, and cannot be accessed from the outside.
I've also tried passing a callback function, but all variables in the callback function are contained within the callback function.
It has come to my understanding that "pass by reference" is not possible with JavaScript.
In the end, what I did was this:
Then I got the response from the DOM, and removed the temporary element.
Is this a "valid" way to do it?
It seems a bit messy..
Thanks for all responses!
Regards, Erich Fosse
I've also tried passing a callback function, but all variables in the callback function are contained within the callback function.
It has come to my understanding that "pass by reference" is not possible with JavaScript.
In the end, what I did was this:
Code:
var APICall = function(settings){
if(settings.error === null){
$.ajax({
type: "GET",
url: settings.url,
cache: false,
dataType: 'json'
}).done(function(response){
$("body").append('<div id="response" style="display: none;" />'); // Adding response to a hidden DOM element
$("#response").attr("response", JSON.stringify(response));
}).fail(function(XMLHttpRequest, textStatus, errorThrown){
// Handle errors here
console.log(XMLHttpRequest);
console.log(textStatus);
console.log(errorThrown);
});
}
}
Then I got the response from the DOM, and removed the temporary element.
Is this a "valid" way to do it?
It seems a bit messy..
Thanks for all responses!
Regards, Erich Fosse