0

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

2
  • 1
    You have an unclosed quote mark. Commented Jul 13, 2012 at 7:17
  • pls ignore that this is just an example for better understanding Commented Jul 13, 2012 at 7:23

2 Answers 2

1

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);        
    });
Sign up to request clarification or add additional context in comments.

6 Comments

ok thnks let me try this i keep in mind the other thing u mentioned thnx
one more thing to ask can u pls write the syntax of callback.call() with paramenters
ok then how will i get this value can u tell me i m calling this function in this way when i pass single variable--> var total; parseData(function (x) { alert(x); });
can u show me any link or any thing to implement ur callback( var1, var2 )
What variables do you need to pass to the callback?
|
1

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.");
        }
   });      

}

1 Comment

pls ignore syntax only the thing i m asking how to return multiple array and varialbe in same as i have done in this callback.call(null,var1); is it possible

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.