unkown
unkown

Reputation: 1128

multiple array return using ajax() on success : jquery

hi friends i have an ajax()of jquery that parse the xml file , create array on success and want to return multiple array on callback is it possible if yes then how ? pls guide me guys. this is my example code .

var arr1= new Array();
var arr2=new Array();
 var var1,var2;
function parseData(callback){   
        var cnt=0;
            $.ajax({
                type:"GET",
                url:"test.xml",
                dataType: "xml",
                success:function(xml){

                    var tempcategory= new Array(2);
                    var tempitem=new Array(4);                  
                    var1= $(xml).find('var1').text();
                    var2= $(xml).find('var2').text();

                    $(xml).find('result').each(function(){
                    var Id = $(this).find('id').text();
                    var Name = $(this).find('name').text();
                        tempcategory[0]=catogoryId;
                        tempcategory[1]=catogoryName;
                        arr1[cnt]=tempcategory;
                        $('#output').append("<br/>"+categories[cnt]);
                         cnt++;
                    });                 
                             callback.call(null,var1);

                error: function(){
                     alert("An error occurred while processing XML file.");
                }
           });      

}

now in this code there is a line callback.call(null,var1);

In this i m returning only 1 variable but like that i have array and some other variable also how can i return that all array and variable togather and if not is there any other way to return multiple array.

thanks

Upvotes: 0

Views: 1068

Answers (2)

Inkbug
Inkbug

Reputation: 1692

What you can do is:

callback.call( null, var1, var2 );

Also, see Harry's answer with the syntax problems in your code.

Note also that it is considered better form to use var arr1 = []; rather than var arr1 = new Array(), they are both equivalent but the first is shorter.

Edit: A much simpler way to do it would be to simply write callback( var1, var2 ). However, if you still want to use .call, see MDN for a full description.

Edit:

thnx for help and we can use it as mentioned in this answer callback.call( null, var1, var2, var3 ); and call the function with

fun_name(function (var1,var2,var3) {        
        alert(var1);
        alert(var2);
        alert(var3);        
    });

Upvotes: 1

Hkachhia
Hkachhia

Reputation: 4539

Syntax error in your function I have corrected it Plz check it

function parseData(callback){   
var cnt=0;
    $.ajax({
        type:"GET",
        url:"test.xml",
        dataType: "xml",
        success:function(xml){
            var tempcategory= new Array(2);
            var tempitem=new Array(4);                  
            var1= $(xml).find('var1').text();
            var2= $(xml).find('var2').text();

            $(xml).find('result').each(function(){
            var Id = $(this).find('id').text();
            var Name = $(this).find('name').text();
                tempcategory[0]=catogoryId;
                tempcategory[1]=catogoryName;
                arr1[cnt]=tempcategory;
                $('#output').append("<br/>"+categories[cnt]);
                 cnt++;
            });                 
            callback.call(null,var1);
        },
        error: function(){
             alert("An error occurred while processing XML file.");
        }
   });      

}

Upvotes: 1

Related Questions