0

I have that code:

var s, d, p = '';

$.ajax(
    {
        type: "POST",
        url: ajaxurl,
        data: {action: "get_info"},
        success: function(r)
        {
            // r contain that json data
            // {"s":"long-string","d":"string","p":"string"}
            // That served from the server with that header
            //
            // header('Cache-Control: no-cache, must-revalidate'); 
            // header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
            // header('Content-type: application/json');

            d = r.d;
            s = r.s;
            p = r.p;
        }
    }
);

// And here console return s as undefined
console.log(s);

Any idea on what's going wrong with that ?

2
  • Why don't you put console.debug(r) at the first point in the success callback and see what returns. Commented Oct 25, 2011 at 9:33
  • 1
    As an aside, did you know variables s and d will be undefined because of the way you've declared them? jsfiddle.net/beneverard/KdDVe Commented Oct 25, 2011 at 10:00

4 Answers 4

5

The $.ajax() call simply starts the ajax operation. The code then falls through to your console.log statement but the ajax call hasn't returned yet. Hence the value of s has not yet been set.

At some point later the ajax call returns your results and at that point your call back is fired. So if you want to refer to the value that is returned, you should reference the variable s inside the callback.

The better way to do this is normally like this:

$.ajax(
    {
        type: "POST",
        url: ajaxurl,
        data: {action: "get_info"},
        success: function(r)
        {

            s = r.s;

            // Do something with s here...
        }
    }
);

If you really need to, you can reference s outside of the callback but if you do, you need to put some mechanism in place to make sure that s has already been initialised (i.e. that your ajax call has already returned). This introduces other complexities such as synchronisation and error handling and can make your program flow rather unreliable.

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

Comments

2

The reason is because when you call $.ajax(...); it returns immediately. The success property specifies a callback that gets called when the AJAX request finishes, and s is only populated there; it's undefined before that.

So basically don't do anything with s before the success callback is launched.

Comments

1

The issue is you think the code executes in the way you have it laid out. It's actually symatically identical to:-

var s, d, p = '';
// And here console return s as undefined
console.log(s);
$.ajax(
    {
        type: "POST",
        url: ajaxurl,
        data: {action: "get_info"},
        success: function(r)
        {
            // r contain that json data
            // {"s":"long-string","d":"string","p":"string"}
            // That served from the server with that header
            //
            // header('Cache-Control: no-cache, must-revalidate'); 
            // header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
            // header('Content-type: application/json');

            d = r.d;
            s = r.s;
            p = r.p;
        }
    }
);

What you probably want is:-

var s, d, p = '';

$.ajax(
    {
        type: "POST",
        url: ajaxurl,
        data: {action: "get_info"},
        success: function(r)
        {
            // r contain that json data
            // {"s":"long-string","d":"string","p":"string"}
            // That served from the server with that header
            //
            // header('Cache-Control: no-cache, must-revalidate'); 
            // header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
            // header('Content-type: application/json');

            d = r.d;
            s = r.s;
            p = r.p;
            // And here console return s as undefined
           console.log(s);
        }
    }
);

Comments

1

Try specifying data type Like type: "POST", url: ajaxurl, dataType: 'json', data: {action: "get_info"},...

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.