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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:38:15+00:00 2026-06-15T15:38:15+00:00

I use the jQuery post requests a lot. I understand that when you are

  • 0

I use the jQuery post requests a lot. I understand that when you are working within the response the variables have their own scope. Is there a good way to set variables in the response but have those variables available outside of the post? Like for other functions in JS.

Here is a sample of what I am doing:

 $.post('URL', { }, function(data) {

    var cData = $.parseJSON(data);

    $.each(cData, function(k, v) {

      var cID = v.id;  

  });

So what I do that I cannot access cID outside of the post request. Is there a way to make this work?

Any help would be great.
Thanks

UPDATE:

Here is a sample I just tested:

var savedCount;

$.post('/app/actions/countsAction.php', { call: "getCountFullByID", countID: countID}, function(data) {

    savedCount = 1;
    alert(savedCount);

});

alert(savedCount);

I get 2 alerts when I run this. The first one is a 1 when the alert is fired off in the $.post and the second one is undefined.

  • 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-15T15:38:16+00:00Added an answer on June 15, 2026 at 3:38 pm

    Just declare your variable outside of the $.post call:

    var cID;
    $.post('URL', function(data) {
        var cData = $.parseJSON(data);
        $.each(cData, function(k, v) {
            cID = v.id;  
        });
    });
    

    …not sure what you’re attempting to do with that though, as you’re looping over a collection and continually (re)setting the value of a single variable. If you need to keep track of all the variables, consider holding the values in (maybe) an Array.

    EDIT
    If you need to do a synchronous (“blocking”) $.post call, you can. From the docs for the asynch function parameter:

    By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: “jsonp” requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

    Cheers

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

Sidebar

Related Questions

I have a standard form in mvc4 operation. I use jQuery.post to perform postback
I am trying to use the return value(response) from the callback outside the jQuery.post
I have a bunch of jQuery AJAX post requests wrapped up in a for
I have a page ( page 1 ) that accepts post requests, does some
i have a file user_submit.php that handle all the POST requests. what i am
I'm working on an ajax application that makes extensive use of jQuery. I'm not
I've got a page that makes a lot of jQuery ajax requests periodically, asking
I use jquery post function to go to server and bring the link to
basically my aim is to use JQuery to post back an x & y
quick question, is it secure to use the jQuery.post function in jQuery to pass

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.