Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6718679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:59:58+00:00 2026-05-26T08:59:58+00:00

I have that code: var s, d, p = ”; $.ajax( { type: POST,

  • 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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:59:58+00:00Added an answer on May 26, 2026 at 8:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have that JavaScript code: var state = 'hidden'; function Show_Picture() { if (state
I have code like that: var xPola = 10, //how many cols yPola =
I have some code that sets the lang var to the site default language.
i have bit of code that causes an underflow: var t1, t2, delta: DWORD:
I have some LINQ code that generates a list of strings, like this: var
I have the following code in specman that I inherited: some_method() is { var
I have code that looks like: //System.Data.IDataRecord dr try { Consolidated = Utility.NullConvert.ToBool(dr[Constants.Data.Columns.cConsolidated], false);
I have code that I want to look like this: List<Type> Os; ... foreach
I have the following code var section = new CustomConfigurationSection(); section.SectionInformation.Type = System.Configuration.NameValueFileSectionHandler; section.SectionInformation.SetRawXml(sectionXml);
I have code that makes an AJAX call to an MVC controller method and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.