javascript - Returned AJAX array not being copied to array -


I have a simple AJAX function that uses jQuery to return an array of 300 test objects from a database.

This data has been copied to a JS array. The code fragment looks like this:

retrieve star locations to display on page $ .getJSON ("stars.php? Jsoncallback =?", Function (data) {for (Var x = 0, xx = data.length; x & lt; xx; x ++) {// planetary array [x] = new planet (data [x] .xpos, data [x] .ips); // Does not work either. (New Planet (data [x] .xpos, data [x] .ypos));}}); {// var i = 0, ii = planetArray.length; i & lt; ii; i ++) {// display data}

Firebug says that Planets around. Long is zero. Planet Constructor looks like this:

  function planet (x, y) {this.x = x; this. Y = y; }  

I think this is a scoping issue, but I can not seem to find it. Creating a new object in other languages ​​means that it exists on the heap and avoids the scope, but here it seems that disappears in ether.

How can I return an array and push it for use in my own array later (or in any other function)?

AJAX request happens asynchronously - you are not waiting for data before you Try and display

Move the display code to the callback and it should work.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -