JavaScript Javascript global variables only accessible after application runs javascript global variables across files,javascript global variables in function,

I have a series of nested Ajax requests to external APIs, which is very ugly but it was the only way I could figure out how to make calls in a specified order with each call utilizing some values brought back from the previous call. (I attempted this but couldn't get it to work, so I reverted to the advice here.)

Anyway, this works well to a point. All my calls work in succession, and I end up with an array called people, which is just a list of names: ["name1","name2","name3"].

My problem is that I don't seem to be able to do anything with this array from my javascript code. I can't append them to a div, nor can I alert them, or even console.log them during code execution. However, once my code completes, I can type people into the browser console and there they all are, as expected.

I am guessing this has something to do with the scope of the variable - I have tried making it global and moving the placement of its declaration, but the only way I can access people from the runnable code is from within the final AJAX loop, and then I get lots of repeating values because it's looping and adding to the array incrementally.

The goal here is to get people from that final API call and list them in HTML.

Here's my code. Any suggestions greatly appreciated.

HTML to trigger event:

<input type='file' accept='image/*' onchange='openFile(event)'>
<!--process is triggered by file upload-->

javascript:

    var openFile = function(event) {

    //... Some UI stuff happens here.
    //... When finished, just call getGraph(); below

    performances = new Array();  // global scope
    people = new Array();  // global scope
    getGraph();  // call function below

    console.log(people);   // retrieve array; doesn't work
  };  

   function getGraph(){ 
    $.ajax({
       url:'http://...' + document.getElementById('prDate').value,
       dataType:'json',
       success: function(response){
           $.each(response, function(i, item) {
               var programID = item.id;
                 $.ajax({
                    url:'http://...'+ programID',
                    dataType:'json',
                    success: function(response){
                        $.each(response, function(i, item) {
                            performances.push( item.id );
                        });
                        $.each(performances, function(index, value){
                            $.ajax({
                               url:'http://...' + this.valueOf() +'/persons/',
                               dataType:'json',
                               success: function(response){
                                   $.each(response, function(i, item) {
                                       people.push( item.firstname + ' ' + item.lastname );  // the magic moment
                                    }); 
                                } 
                            }); 
                        }); 
                    }
                });
            });
        }
    }); 
} 
Answer:1

From your code it is visible that people variable will be create only once you call openfile function. If you want it be created even when the openfile method is not called then declare it outside of all the functions and then it will be accessible or else declare it in the place where you intend to use it like above the ajax call, then use it.

Answer:2

I'm trying to get all callable phone numbers out of an html-received with a get: onload: function (data) { data = $.parseHTML(data.response); var content = $.trim($(data).find('[href^=callto:]...

I'm trying to get all callable phone numbers out of an html-received with a get: onload: function (data) { data = $.parseHTML(data.response); var content = $.trim($(data).find('[href^=callto:]...

  1. jquery find href value
  2. jquery find href contains string
  3. jquery find href containing text
  4. jquery find href
  5. jquery find href attribute
  6. jquery find href in div
  7. jquery find href by text
  8. jquery find href starts with
  9. jquery find href by url
  10. jquery find href by id
  11. jquery find href inside div
  12. jquery find href and replace
  13. jquery find href with class
  14. jquery find href in td
  15. jquery find href in li
  16. jquery find href by title
  17. jquery find by href attr
  18. jquery find all href
  19. jquery find where href equals
  20. jquery find anchor href contains

I have the following code in a SharePoint aspx page ( I got an error that said I cannot use form controls... that is why the form tags are not there): <div id="formBox"> Here is a link : <a ...

I have the following code in a SharePoint aspx page ( I got an error that said I cannot use form controls... that is why the form tags are not there): <div id="formBox"> Here is a link : <a ...

  1. post without using form
  2. validation without using form
  3. jquery validation without using form tag
  4. submit form without using form tag
  5. javascript validation without using form
  6. upload-file-without-using-form-submit
  7. submit data without form using ajax
  8. php upload file without using form
  9. setting post variable without using form

I'm trying to find the most graceful way to distinguish between single and double clicks in Bacon.js, but I feel like I'm not completely grasping how this works. The following works for detecting a ...

I'm trying to find the most graceful way to distinguish between single and double clicks in Bacon.js, but I feel like I'm not completely grasping how this works. The following works for detecting a ...

I have a input that have type like this: <input class="emailSend" name="emailSend" type="hidden"> Then I have a multiple select option like this <div class="controls"> <select ...

I have a input that have type like this: <input class="emailSend" name="emailSend" type="hidden"> Then I have a multiple select option like this <div class="controls"> <select ...

  1. fill input type file javascript
  2. fill input type date
  3. fill input type file
  4. auto fill input type file
  5. pre fill input type date
  6. jquery fill input type file
  7. jquery fill input type text
  8. jquery fill input type date
  9. js fill input type file