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

  • SEARCH
  • Home
  • 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 7958103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:22:08+00:00 2026-06-04T04:22:08+00:00

Any way of using the data_response outside the $.post() ? This is part of

  • 0

Any way of using the data_response outside the $.post()?

This is part of the code I use:

$.post('do.php', { OP: "news_search", category: cat_id },
    function(data_response){
        var response = data_response; //I need to access this variable outside of $.post()
    }
}, "json");

console.log(response); //response is not defined, is what I get for now

UPDATE

Is there no way of getting that response available globally?

  • 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-06-04T04:22:08+00:00Added an answer on June 4, 2026 at 4:22 am

    No; $.post executes asynchronously, so when you call console.log, the AJAX request is still running and hasn’t yet yielded a response. This is the purpose of the callback function: to provide code to be run after the request has completed. If you move console.log into the callback function, it should work:

    $.post('do.php', { OP: "news_search", category: cat_id },
        function(data_response){
            var response = data_response; //I need to access this variable outside of $.post()
            console.log(response);
        }
    }, "json");
    

    Update: If you want the response data to be globally available, you can declare the variable in the global scope like so:

    var response = null;
    $.post('do.php', { OP: "news_search", category: cat_id },
        function(data_response){
            response = data_response;
            console.log(response);
        }
    }, "json");
    

    Of course, the only context in which you can be sure that response has
    actually been populated with a value is in the callback function supplied to
    $.post after the line response = data_response;. If you want to use it at
    any other stage in the script then you’ll have to check its value first;
    something like this:

    if (response !== null)
    {
        console.log(response);
    }
    

    Just be aware that this code won’t do anything if you put it straight after
    the $.post call; it’ll only be useful if it’s executed after the POST request has finished, in some other asynchronous callback (perhaps a UI interaction event of some kind).

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

Sidebar

Related Questions

Is there any way to differentiate IE7 versus IE6 using PHP's get_browser() function?
Is there any way for using an annotation for running a function before currently
Sorry for this silly question, but is there any way to restrict using directives
Is there any way to upload an entire folder using PHP?
I am using this code if form.is_valid(): form.save() if request.POST.get('ajax') == 'true': return HttpResponse('Data
Is there any way using foursquare api or some other location based api to
Is there any easy way using the api to get a count of all
Is there any way by using JDT ASTParser, by which we can identify if
Is there any way to validate using DataAnnotations in WPF & Entity Framework?
is there any way to avoid using tmp table? I am using a query

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.