2

Loading in a bunch of json files from an array called bunchOfData. The .url is the url for each json file.

How do I read my variable "myI" in the processData function?

for(var i = 0; i < bunchOfData.length; i++){
        $.getJSON(bunchOfData[i].url, {"myI":i}, function(ds){}).success(processData);

        function processData(ds){
            console.log(ds.myI); //undefined
        }
    }
7
  • 1
    Could you be more specific. Post your json object. Commented Mar 7, 2013 at 20:50
  • 1
    Have you tried console.log(arguments) ? Commented Mar 7, 2013 at 20:51
  • 2
    Do you want to get the myI value you sent inside processData? Commented Mar 7, 2013 at 20:51
  • @RocketHazmat - yes. I want it inside processData. The processData function only fires if the data gets fired if the json is loaded successfully. Commented Mar 7, 2013 at 20:56
  • @Shanimal yes, I see 2 objects and a "success." But no myI. Commented Mar 7, 2013 at 20:57

2 Answers 2

1
function successHandler(i){
    console.log('i is instantiated/stored in the closure scope:',i);
    return function(data, textStatus, jqXHR){ 
        // here, the server response and a reference to 'i'
        console.log('i from the closure scope:',i, 'other values',data,textStatus,jqXHR);
    }            
}

var i = 0; i < bunchOfData.length; i++){
    $.getJSON(bunchOfData[i].url, {"myI":i}, function(ds){}).success(successHandler(i));

}
Sign up to request clarification or add additional context in comments.

22 Comments

Okay, so that allows me to get the correct "i" but doesn't send my ds (dataset). If I do handler(ds, i), it doesn't know what ds is. Anyway for me to pass the i and the ds in that handler function?
Change return function(){ to return function(ds){.
@RocketHazmat ? "return function" isn't written anywhere. It's just function(ds)
@SShazboticus: Inside function handler(i){.
You'll have to write it out. I'm not sure what you're saying.
|
0

You cannot access varibles outside of scope, the response will only be valid

function(ds){
    // Here
}

Try this:

for(var i = 0; i < bunchOfData.length; i++){
    $.getJSON(bunchOfData[i].url, {"myI":i}, function(ds){
        console.log(ds);
    });
}

Also once the request is finished and was successfull contents of success will run, note that what you are trying to achive can be dont with a single request (you can unify the files and serv them in a single one), so you get the data and then iterate.

4 Comments

Unfortunately, that doesn't work either. One of the first things I tried. =/
What does the console say XMLHttpRequests? whats the response for the request?
console says object for each one and I can view the contents. (Chrome)
if you can view the contents this means the console.log(ds); outputs the object and ds contains your data you just need to use the correct keys...

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.