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 6968047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:24:02+00:00 2026-05-27T16:24:02+00:00

Here is the initial code: var res = null; $.post(‘test.php’, ‘q=testdata’, function(response) { res

  • 0

Here is the initial code:

var res = null;
$.post('test.php', 'q=testdata', function(response) {
   res = response;
});

I need to use res at some point in my code, or right after the ajax call. I tried this:

var res = null;
$.post('test.php', 'q=testdata', function(response) {
   res = response;
   alert('Note 1: '+res); /* Returned exactly what I wanted */
});
alert('Note 2: '+res); /* Returned null. I need to use res here. How? */

How is it possible to use res to hold the desired value after the ajax call?

  • 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-27T16:24:03+00:00Added an answer on May 27, 2026 at 4:24 pm

    Your code makes an asynchronous ajax request while the second alert executes synchronously. This means that a response may not be available when the (sequentially) second alert executes.

    Try putting this behind your $.post call: $.ajaxSetup({async:false}).

    It will force the alerts to fire in the order that they appear in your code. If an async call is what you want to make, then I suggest you follow Sudhir’s advice.

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

Sidebar

Related Questions

I'm trying to extract functions and function headers from some source code files. Here's
Here is my code: <!DOCTYPE html> <html> <head> <meta charset=utf-8> <meta name=viewport content=width=device-width, initial-scale=1>
Here is my code: SqlConnection conn4 = new SqlConnection(Data Source=.\\sqlexpress;Initial Catalog=test_BdbCSSQL01;Persist Security Info=False;Integrated Security=SSPI;);
My initial question was here and was related to the postgres backend. Postgres subquery,
Here is my code, which takes two version identifiers in the form 1, 5,
here is my code - I've tried using the google maps api and other
I am using Google maps JavaScript API. Here is code : <!DOCTYPE html PUBLIC
The Application In PHP, I will output some initial views so web crawlers can
Does anyone have experience detecting identical text(); and class? here is my current code
Here's a basic regex technique that I've never managed to remember. Let's say I'm

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.