1

I declared a global variable var idCategories= new Array(); in my file.js I use it in this function

function test() {
    $.ajax({
        type: "GET",
        url: "http://my_site/api/categories?ws_key=" 
             + ws_key  
             + "&PHP_AUTH_USER=" + PHP_AUTH_USER,
        dataType: "xml",
        success: parseXml
    });
    function parseXml(xml) {
        var i = 0;
        $(xml).find("category").each(function () {
            idCategories[i] = $(this).attr('id');
            // length increments at each iteration
            alert("length=" + idCategories.length);                 
            i = i + 1;
        });
    }
    alert("length=" + idCategories.length); //returns 0
}

in the function parseXml(xml) the array length is well incremented but outside of this function length = 0! so that I can't use the array idCategories in another function!

1
  • Can you post the entire example, including the global variable declaration? Or create a jsFiddle that reproduces the issue? Commented Mar 18, 2012 at 11:59

5 Answers 5

3

The problem is that the AJAX call is ASYNC. So parseXml will most likely, if not always, be called after your alert is called.

Why not:

function test() {
    $.ajax({
        type: "GET",
        url: "http://my_site/api/categories?ws_key=" 
             + ws_key  
             + "&PHP_AUTH_USER=" + PHP_AUTH_USER,
        dataType: "xml",
        success: parseXml
    });
    function parseXml(xml) {
        var i = 0;
        $(xml).find("category").each(function () {
            idCategories[i] = $(this).attr('id');
            // length increments at each iteration
            alert("length=" + idCategories.length);                 
            i = i + 1;
        });
        alert("length=" + idCategories.length); //returns 0
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

$.ajay is by default asynchronus function! That means thet when it starts to execute it does not block application flow. you're alert statement executes before $.ajax success function. You have two solutions.

  1. set asny parameter to false.

    $.ajax({
        type: "GET",
        async: false, 
    

    ...

  2. call alert in parseXml function.

I belive you're best bet is async:false, but correct way of doing it would be to advance script execution after $.ajax call is finished (execute next step in parsexml function).

4 Comments

don't use async = false its the same as var wait = true;while(wait); when ajax is done wait is set to false
BUT synchronous calls are bad since they lock up the browser.
Advance youre execution after async call is finished!
@elrado: this is exactly what I want, but this is also my problem!!
1

try to using object and array

function parseXml(xml) {var obj={};var a = [];
  $(xml).find("category").each(function(i)  {
obj["idCategory"]= $(this).attr('id');
a.push(obj);
alert("length="+a.length);// length increments at each iteration
       });
}
alert("length="+a.length);//returns 0

}

Comments

0

This question appears multiple times a day. You are making an asynchronous call, that alert line fires BEFORE the Ajax call is returned.

3 Comments

Read the value in the callback function where you do the processing.
But I want to use it in other function also!
You have to code your code to wait for it to update, welcome to the world of asynchronous coding. Callbacks are meant to handle this stuff. Look into api.jquery.com/category/deferred-object
0

Ajax is asyncronous. Your alert("length="+idCategories.length); code is called before response is delivered.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.