2

Following is my JavaScript. How do I make the variable success global so that the changes made in ajax->success are reflected outside?

function foo() {  
    var success = false;  
    $.ajax({

        type: "POST",
        url: "",
        dataType: "xml",
        success: function(xml) {
            var code = parseInt($(xml).find("Response").attr("code"), 10);
            switch (code) {
                case 1:
                    success = false;
                    break;
                case 0:
                    success = true;
                    break;
            }
        }
    });
    return success;
}
7
  • 1
    I believe if you are declaring it outside any function , it is a global variable. It will be something like window.success !!! Commented May 16, 2013 at 4:57
  • Not sure what exactly you're trying to achieve but the variable "success" is placed properly and should work. Besides, if you're returning "success", it means you're using it in a function, making a private variable for that specific function. If you want to make it a global variable, simply put it at the very start of you're script, or just right outside this function. Commented May 16, 2013 at 4:58
  • 1
    This is literally the most asked question in JavaScript SO. It's all in the name: AJAX or Asynchronous JavaScript and XML. Commented May 16, 2013 at 5:05
  • possible duplicate of How to return the response from an AJAX call? Commented May 16, 2013 at 5:06
  • 1
    @RobG success = !code. Ian got this right Commented May 16, 2013 at 5:11

2 Answers 2

4

Pass a callback function that is called in the success:

function makeCall(callback) {
    $.ajax({
        type: "POST",
        url: "",
        dataType: "xml",
        success: function(xml) {
            var code = parseInt($(xml).find("Response").attr("code"), 10);
            callback(!code);
        }
    });
}

makeCall(function (success) {
    alert(success);
});

This is how asynchronous programming/requests work. Of course, the alternative is to make it a synchronous request, but kind of defeats the purpose.

I condensed the switch stuff because you seemed to want the opposite boolean values of 0 and 1.

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

Comments

0

The approach you are after will not work. The Ajax call is asynchronous. Meaning it will return immediately even before the actual GET is fired. So the outer var (which is accessible from inside the success callback, thanks to closures) won't have any value other than the one established before the Ajax call by the time it is returned. Instead you can specify that the Ajax call fire synchronously, by setting async:false along with the other values in the $.ajax, or you can restructure the code to do whatever you wanted to do if the function returned true by putting it inside the success callback.

4 Comments

I was thinking of upvoting you until I saw you recommend async:false
async:false is to ajax as eval is to string?
@elclanrs no. eval merely invites injection attacks and is slow as hell. async:false is pure evil ;-)
I agree, I was just trying to be complete :)

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.