0

I have a function that pull information from an XML file located on the system. IT will then pull the values located in that file and put them into an array. Once the function is called the values enter the array, but then onnce the function ends the values go away.

 function getXML(location,day){
            $(document).ready(function () {
                $.ajax({
                    type:'post',        //just for ECHO
                    dataType: "xml", // type of file you are trying to read
                    crossDomain:true,
                    url: './../CurrentFiles/'+ location +'.xml', // name of file you want to parse
                    success: function (xmldata){
                        if(array[0] == null){
                            $(xmldata).find('dgauges').children().each(function(){
                               array.push($(this).text());
                             });
                        }
                     }, // name of the function to call upon success
                    error: function(xhr, status, error) { 
                        console.log(error);
                        console.log(status);
                    }
                });
            });

            return array[day];
        }

From what I researched it could be a problem with async, but I do not understand entirely what that is. Also I am very new to jquery so if there is any thing that seems out of place that's why.

THis is what my plan is for this function

I have an XML file formatted like

<dgages><d>26.850</d><d-1>7.70</d-1><d-2>2.00</d-2><d-3>27.90</d-3></dgages>

I am trying to pull all of those values in an array so I can do some calculations on them.

  1. Get the XML Document
  2. find all the children of dgage
  3. put each of the children into the array 4 once the array is filled return the associated day.
  • there is no start scope for `array`, this function may or may not return what you want, depending on when it's called (`$(document).ready(`) ; Also ajax is asyncron, so you probably return `array[ret]` before the ajax call actually happend - therefor its undefined. I've rarely seen so much wrong in a single function body :( – birdspider Aug 05 '15 at 12:36
  • I am not getting what you are doing but guessing from common error people commit. write "return array[day];" at the end of your success method. – Jayesh Aug 05 '15 at 12:37
  • The array is located outside of the function so that some other functions can access the data if need be with only calling the function once to get all of the data. – James Knots Aug 05 '15 at 12:49

1 Answers1

0

Try making a synchronous call instead. AJAX calls are async by default, which means that code will jump to the next line before waiting for the result of the previous line. You can enforce the result by telling the AJAX call to do it synchoronously:

function getXML(location, day) {
    var array = [];
    $.ajax({
        type: 'post', //just for ECHO
        dataType: "xml", // type of file you are trying to read
        crossDomain: true,
        async: false, // Wait until AJAX call is completed
        url: './../CurrentFiles/' + location + '.xml', // name of file you want to parse
        success: function(xmldata) {
            if (array[0] == null) {
                $(xmldata).find('dgauges').children().each(function() {
                    array.push($(this).text());
                });
            }
        }, // name of the function to call upon success
        error: function(xhr, status, error) {
            console.log(error);
            console.log(status);
        }
    });

    return array[day];
}
Kerem
  • 317
  • 1
  • 14
  • I tried that in an earlier version of this function but It still did not work. If I removed the $(document).ready() would that solve the issue? – James Knots Aug 05 '15 at 12:46
  • You should totally remove $(document).ready(), and also initialize array variable. Check my updated code. – Kerem Aug 05 '15 at 12:55
  • Could I add an statement into this array to make it the array global? – James Knots Aug 05 '15 at 12:57
  • Yes, you just have to define it before you use it. Can be a local variable, or a global variable. Read this to have a better understanding of JS variable scope: http://stackoverflow.com/a/500459/1139130 – Kerem Aug 05 '15 at 13:00
  • I want it global so the first time it will load the values and return the value, but anytime after that is wil just load the value. – James Knots Aug 05 '15 at 13:02
  • Then you put array variable in global scope, and check if it is loaded before you make the AJAX call. – Kerem Aug 05 '15 at 13:53