Im still somewhat of a newbie on jQuery and the ajax scene, but I have an $.ajax request performing a GET to retrieve some XML files (~6KB or less), however for the duration the user spends on that page that XML content should not / will not change (this design I cannot change, I also don't have access to change the XML file as I am reading it from somewhere else). Therefore I have a global variable that I store the response data into, and any subsequent look ups on the data are done on this variable so multiple requests don't need to be made.
Given the fact that the XML file can increase, Im not sure this is the best practice, and also coming from a java background my thoughts on global public variables are generally a no-no.
So the question I have is whether there might be a better way to do this, and a question on whether this causes any memory issues if the file expands out to some ridiculous file size?
I figure the data could be passed into a some getter/setter type functions inside the xml object, which would solve my global public variable problems, but still raises the question on whether I should store the response inside the object itself.
For example, what I currently do is:
// top of code
var xml;
// get the file
$.ajax({
type: "GET",
url: "test.xml",
dataType: "xml",
success : function(data) {
xml = data;
}
});
// at a later stage do something with the 'xml' object
var foo = $(xml).find('something').attr('somethingElse');
I really struggled with getting the results of jQuery ajax into my variables at the "document.ready" stage of events.
jQuery's ajax would load into my variables when a user triggered an "onchange" event of a select box after the page had already loaded, but the data would not feed the variables when the page first loaded.
I tried many, many, many different methods, but in the end, it was Charles Guilbert's method that worked best for me.
Hats off to Charles Guilbert! Using his answer, I am able to get data into my variables, even when my page first loads.
Here's an example of the working script:
your problem might not be related to any local or global scope for that matter just the server delay between the "success" function executing and the time you are trying to take out data from your variable.
chances are you are trying to print the contents of the variable before the ajax "success" function fires.
You might find it easier storing the response values in a DOM element, as they are accessible globally:
This has the advantage of not needing to set async to false. Clearly, whether this is appropriate depends on what you're trying to achieve.
You don't have to do any of this. I ran into the same problem with my project. what you do is make a function call inside the on success callback to reset the global variable. As long as you got asynchronous javascript set to false it will work correctly. Here is my code. Hope it helps.
Hope this helps you .
.get responses are cached by default. Therefore you really need to do nothing to get the desired results.