Storing AJAX Response As Local Varaible - One Call For Multiple Tables
Storing AJAX Response As Local Varaible - One Call For Multiple Tables
I have a working example and a non-working example and I'm trying to figure out why I'm having issues with one and not the other.
My goal is to be able to make a single AJAX request, store that data, and publish the value to multiple tables rather than retrieving data from a URL each time a table is populated
Example 1 is using an arbitrary API I found online:
Example 1: http://live.datatables.net/coxuyica/1/edit
This appears to work as described. An ajax call is made and returned and stored as a variable which is then set as the data option.
Example 2 is using datatables sample data from http://live.datatables.net/examples/ajax/data/objects.txt
But when I use the same method as above, the table doesn't populate:
Example 2: http://live.datatables.net/mubesena/1/edit
My guess is it has something to do with how the objects.txt file has all of the data nested inside of an array called "data" and the other API output does not present the data in the same way.
Assuming this is the case, what do I need to change to Example #2 to have it read the data in appropriately?
Answers
Finally figured it out.
EDIT: And I guess I should have asked in the first place. I'm only assuming, but is making a single call and storing the data as a variable more efficient than making multiple calls? I assume this would be mostly tied to network latency and local memory needs.
When using the
data
not only did I need to reference the variable, but also the object within the variable since all of the data was nested within this object.So with a variable named dataset, instead of
data: dataset
I needed to dodata: dataset.data
to reference the object inside of that variable.Also needed to add JSON.Parse(data) when putting the data to a variable.