Brandon Meredith
Brandon Meredith

Reputation: 135

Import an array using ajax

I have a very simple 2D array in a file called synergy_data.json:

[
['2014-08-19',  2956],
['2014-07-19',  2038],
['2014-06-19',  1285],
['2014-05-19',  1383],
['2014-04-19',  1256],
['2014-03-19',  822],
['2014-02-19', 644],
['2014-01-19',  504],
['2013-12-19',  438],
]

I'm trying to call up that data using ajax:

data=[]
$.ajax({
    url: "/marquee/synergy_data.json",
    async: false,
    success: function(resultData) {
        data = resultData;
    }
});
console.log(data)

But the console only spits out "[]"

What am I doing wrong?

***Solution A big thanks to @Quentin for solving this for me. I had two fatal flaws with my JSON: 1) I used single quotes and 2) I had an extra comma. I'll be sure to use JSONLint to check my arrays in the future! (I also removed the "async:false" for good measure.)

Upvotes: 2

Views: 121

Answers (1)

Quentin
Quentin

Reputation: 944432

Your JSON is invalid. The success function won't fire. The error function isn't defined. You end up accessing the array you originally assign to data and not the one from the JSON.

Strings in JSON must be quoted using " characters. ' are not acceptable.

Commas are used to separate items in an array, not terminate them. Remove the trailing comma.

Upvotes: 3

Related Questions