0

Right now I have the code:

$.get("test.php", function(($cars){
    $cars = cars;
}, "json");

$(function(cars){
    $.each(cars, function(i, startList){
        if(iNLarr[i]){
            iNLarr[i](startList.name);
        }
    });
});

Ok I'm assigning the $cars array to the cars variable. Basically $cars is an array of objects, each object is an associative array. I need to be able to work on the array so I assigned it to the variable first. The next function uses the information in cars and displays it in html elements. iNLarr is an array of functions each function changing a replacing name html elements with the name retrieved from the $cars array of objects.

Whenever I run the function I get something back like array.prototype.map is not a valid parameter. I am thinking that startList is a made up parameter that I was hoping would be assigned to the objects so I could retrieve each name, but I'm guessing that's not the case, can anyone help me.

I'm alos not sure I've defined the function properly.

Kevin
  • 41,694
  • 12
  • 53
  • 70
Tom
  • 57
  • 6

3 Answers3

1

Use the below code to accomplish your task :

$.get("test.php", function(($cars){
    cars = $cars;
    $.each(cars, function(i, startList){
        if(iNLarr[i]){
            iNLarr[i](startList.name);
        }
    });
}, "json");

Tell me if need any explanation.

afzalex
  • 8,598
  • 2
  • 34
  • 61
  • This is only half the problem because the following code with the `$.each()` probably tries to use the `cars` variable before the `$.get()` has finished. That code needs to be moved into the `$.get()` completion callback and restructured a bit. – jfriend00 Sep 20 '14 at 11:27
  • Then he must add a callback function which will be called when ajax request successfully complete. – afzalex Sep 20 '14 at 11:29
  • There already is a callback, the second piece of code just needs to be moved into it and take `cars` out of `$(function(cars){`. – jfriend00 Sep 20 '14 at 11:30
  • Thank you for helping. In hurry I didn't even read his complete question – afzalex Sep 20 '14 at 11:32
  • I'm very new, but I would have thought it just loaded the functions in order. and what exactly is a callback function. – Tom Sep 20 '14 at 11:35
  • @Tom callback function is one which will be called after the `$.get()` has completed its request (not simultaneously but after the request is completed). – afzalex Sep 21 '14 at 13:18
1

At the moment neither function makes sense. This line in the first:

$cars = cars;

will overwrite the result of your ajax call with an undefined (at least, undefined in the code shown) variable cars. And then you don't actually do anything further with $cars - no point giving it a value if you don't use it.

Then your second function is getting bound as a document ready handler which means the cars argument will be set to reference jQuery. That is, when you call $() or jQuery() and pass a function as an argument:

$(function() { /* some code */ });

...that function will be called when the DOM is ready.

You should do all of the processing you need with your ajax call within the first function, something like this:

$.get("test.php", function(cars){
    $.each(cars, function(i, startList){
        if(iNLarr[i]){
            iNLarr[i](startList.name);
        }
    });
}, "json");

You may need to wrap the above in a document ready handler, or put it in a script at the end of the body.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • I said it before, I need to be able to work with the code I get from the ajax call later once the page is loaded so I tried separating it. – Tom Sep 20 '14 at 11:59
0

The $(function () { … } notation is for executing code at document ready/load time.

Your $.get is done at script loading time (begore document loaded), but the response can come after the document loading.

$(function() {
    $.get("test.php", function(cars) {
        $.each(cars, function(i, startList) {
            if(iNLarr[i]){
                iNLarr[i](startList.name);
            }
        });
    }, "json");
});

May it help you.

Nicolas Albert
  • 2,586
  • 1
  • 16
  • 16
  • that's a correct answer, but I've already had code similar, what I need to do is separate it so I can work with the array I get back from the php. This is going to be a function that works onload, but after that I want to be able to change the html content in a similar manner so I put the php in a variable that I can work with. – Tom Sep 20 '14 at 11:45
  • Not 100% ideal because it waits to even start the ajax call until the document is loaded which could be slower than starting it right away and only processing the results if/when the document is ready. – jfriend00 Sep 20 '14 at 11:45
  • @Tom, then you have to put the code that works on the `cars` array into the callback function or call a function from there and pass it the `cars` data. You cannot do it the way you originally had it. See [this answer](http://stackoverflow.com/a/14220323/816620) for a lot more detail on why. – jfriend00 Sep 20 '14 at 11:47
  • @jfriend00, that was really helpful, I'm still not completely clear on it, but the basic idea is that I set the I make any function reference the callback in the code. It woud probably work, but I really need to work with the data I receive from the ajax request. Like say I want to filter or sort the objects in the array then place them on screen. It shouldn't be a problem in some way as long as I can store the ajax data in a variable in some way. I've got an idea, i'll give it a try. – Tom Sep 20 '14 at 12:23