1

I'd like to store a value that is correctly fetched via (NON ASYNC) ajax call (from facebook graph api) back to my javascript multidimensional array.

Everything works fine until the part when the data that I get from facebook api has to be stored to my previously defined 2d-array (and afterwards printed on the page via jquery append). I simply can't figure out where's the problem.

Here's the code sample:

Defining 2d array and its values:

fbpages = new Array (13);
i=0;
for (i=0;i<=12;i++) fbpages[i]=new Array(4);

fbpages[0][0] = "http://graph.facebook.com/120830347930692?callback=?"; 
fbpages[0][1] = "Kupi";
fbpages[0][2] = "/vijesti/tag/kupi/";
fbpages[0][3] = 0;

fbpages[1][0] = "http://graph.facebook.com/214014840762?callback=?";
fbpages[1][1] = "Kolek";
fbpages[1][2] = "/vijesti/tag/kolek/";
fbpages[1][3] = 0;

etc...

Fetch the data for every page using the URL from the array fbpages[x][0] and store it back to the same array, to the field fbpages[x][3]:

y=0;
for (y=0;y<=12;y++){
    pageURL = fbpages[y][0];
    fetchData(y,pageURL);
};

function fetchData (index,fbpageurl) {
        $.ajax({
            type: "GET",
            url: fbpageurl,
            dataType: "json",
            async: false,
            success:function(data){fbpages[index][3]=data.likes;}
        });
};

Data printing works fine.

Thanks in advance!

3
  • 1
    I'm confused about the actual problem if the data printing works fine. Looking at your code, it should work correctly. Commented Mar 19, 2011 at 17:06
  • I'm with @dmcnelis but you could try to refactor and simplify your loops to reduce the possibility for errors. E.g. for (var y=0;y<=12;y++) will do fine, no need to set y to 0 before starting the loop while defining the variable using var is highly recommended. Also, I'd recommend using push instead of defining the length of the arrays before filling them. Like this you can avoid empty elements in case something goes wrong. Commented Mar 19, 2011 at 21:35
  • data printing is ok but the problem is that it prints out 0 (from the fbpages[someindex][3]) instead of the new value I get from ajax call (data.likes). So it obviously means that the data I got is not stored back to that array (but it is fetched correctly since on alert it prints correct values). Commented Mar 20, 2011 at 18:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.