0

Defining a Global variable in JavaScript/jQuery and getting it later in script.

But strange it is not working.

printing getResult in console giving me undefined.

but when trying to print getResult just after the assigned value to it, it is giving me accurate value.

Actually i need ajax result at the bottom .. how could i do this..?

var getResult;
$.getJSON(url, function(data) {
    //JS statement to Ajax
    //JS statement to Ajax  
    $.ajax({
        type: "post",
        dataType: "json",
        url: urlTo,
        data: dataSend,
        success: function (result, status) {
                getResult = result; 
                console.log(getResult); // getResult is working here
        }
    });

    //JS statement to Ajax
    //JS statement to Ajax  
    $.ajax({
        type: "post",
        dataType: "json",
        url: urlTo2,
        data: dataSend2,
        success: function (result2, status) {

        }
    });

    console.log(getResult); // getResult is not working here. Giving " undefined" result here.
});
1
  • ok fine. i understand what i am doing.. i need ajax result at the bottom .. how could i do this..? Commented Jul 24, 2014 at 18:03

3 Answers 3

4

JavaScript is synchronous. AJAX is asynchronous.

Therefore, the console.log(getResult); at the bottom is firing before the following AJAX executes:

success: function (result, status) {
    getResult = result; 
    console.log(getResult); // getResult is working here
} 

You can give getResult a default value and test it yourself.

Sign up to request clarification or add additional context in comments.

3 Comments

What exactly do you mean by "i need ajax result at the bottom"? Are you wanting it in the 2nd AJAX success function? Are you trying to do something after both AJAX calls are completed? What?
Actually i want to store ajax result in variable and then want to use for other purpose.. we can say i want use these ajax result to pass to Template using Underscore.js
Ok, well like I said. JavaScript is synchronous, so you can't just assume your asynchronous AJAX will have returned by the time you get to the bottom. So, typically, what you have to do is call a function from inside your AJAX success function and that function will use the global variable you set.
4

That is because getResult is not defined until the AJAX call is done, it is only declared.

Since AJAX is async, your first console log is like that :

var getResult;
console.log(getResult);

Can you give me the value of getResult here? No, because it is undefined (well actually its value is undefined, shhh)

2 Comments

ok fine. i understand what i am doing.. i need ajax result at the bottom .. how could i do this..?
You could use a function to do something with your value : jsfiddle.net/3Fe8Q/3
1

On the second AJAX call, you did not assign getResult = result2; causing "undefined" value.

Also, put the global var = getResult; inside the getJSON function.

Comments

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.