0

I do an AJAX where I get a JSON object (questions) :

function questionnaire()
{
    $.ajax({

        type: "POST", 
        dataType: "json",
        url: "/configuration/nacellesquestionnaire",
        success: function(questions)
        {
            displayQuest(questions);

            return questions;
        },
        error: function()
        {
            return false;
        }
    });
}

So , with the return questions, I want to put this JSON object in an Array :

$(document).ready(function()
{
    var questBackup;
    questBackup = questionnaire();
});

The problem : When I check the array questBackup with a console.log(), there are an "undefined" ...

Note : when I do console.log(question) in success AJAX I have the correct JSON object.

Yvan
  • 61
  • 4
  • 1
    AJAX here is not a synchronous call and you will not get the response of url request directly on method call. I would suggest you to read on ajax calls before you proceed further. – Subir Kumar Sao Jul 04 '16 at 10:04
  • 1
    you are making asynch ajax call so before the questionnaire() method return's the object the calling function prints questBackup hence undefined. Instead call questionnaire() in document.ready and then call a function from success of ajax request to save data. – Shashank Sharma Jul 04 '16 at 10:08
  • Thank for your comments ! I have solve the problem with a callback function. – Yvan Jul 04 '16 at 11:32

0 Answers0