0

I have a problem with returning data from ajax to caller function. When I'm console.logging it out it's undefined.

I believe my problem occurs due the fact that js is asynchronous, and when I'm console.logging the data out it's not ready yet. What can I do to solve it?

FooFunction: function(userInput){

    var fooData = FooFunction2(userInput);
    console.log(fooData);     // <--- undefined
},

FooFunction2: function(userInput) {

    $.ajax({
         url:'./php/test.php',
         type:'post',
         dataType:'json',
         data:{ 
             fooData: userInput
         },
         success:function(data) {
             ...manipulating the data...

             console.log(manipulatedData);    // <--- ['foo', 'foo2'];
             return manipulatedData;
         }
    });
},
2
  • Please show what console.log(data); shows. Commented Dec 29, 2012 at 14:34
  • from where does it comes and where is defined userInput ?? you are saying nothing with this example, you should check where that var starts Commented Dec 29, 2012 at 14:50

2 Answers 2

2

The ajax call is asynchronous, so returning will not work. Change your code to use a callback that is invoked when the ajax call has completed.

I changed your code to do that:

FooFunction: function(userInput){
    var callbackfunc = function(ajaxData)
    {
        console.log(ajaxData); //ajax is complete!
    };

    this.FooFunction2(userInput, callbackfunc);
},

FooFunction2: function(userInput, callbackfunc) {

    $.ajax({
         url:'./php/test.php',
         type:'post',
         dataType:'json',
         data:{ 
             fooData: userInput
         },
         success:function(data) {
             ...manipulating the data...

             console.log(manipulatedData);    // <--- ['foo', 'foo2'];
             callbackfunc(manipulatedData);
         }
    });
},
Sign up to request clarification or add additional context in comments.

Comments

1

FooFunction2 is property of object use this.FooFunction2

and you cant return from asynchronous methods. either make ajax call synch or proivde callback.

FooFunction: function(userInput){

    var fooData = this.FooFunction2(userInput);
    console.log(fooData);     // <--- undefined
},

Modified Code

FooFunction: function(userInput){

     this.FooFunction2(userInput, function(fooData){
          console.log(fooData);     // <--- undefined
    });

},

FooFunction2: function(userInput, cb) {

    $.ajax({
         url:'./php/test.php',
         type:'post',
         dataType:'json',
         data:{ 
             fooData: userInput
         },
         success:function(data) {
             ...manipulating the data...

             console.log(manipulatedData);    // <--- ['foo', 'foo2'];
             cb(manipulatedData);
         }
    });
},

1 Comment

@undefined: Yes, it does. The way to solve not being able to return from an asynchronous function is to use a callback instead.

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.