javascript - Variables set in response of jQuery post request - Stack Overflow

I use the jQuery post requests a lot. I understand that when you are working within the response the va

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.

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.

Share edited Dec 7, 2012 at 20:12 Sequenzia asked Dec 7, 2012 at 19:58 SequenziaSequenzia 2,3719 gold badges40 silver badges60 bronze badges 3
  • The bottom alert fires immediately after the request is sent to the server, before the response is received. So yes, it will be undefined. The alert in the $.post callback will happen after savedCount is set, and will be one. At that point, the value of savedCount outside of the $.post call is set as well. – Madbreaks Commented Dec 7, 2012 at 20:15
  • it's because ajax is asynchronous.. so the second alert is firing before the post function successfully pletes - causing the undefined.. – wirey00 Commented Dec 7, 2012 at 20:17
  • 1 How are you using that cID variable? This may be an issue with asynchronous callbacks, where the code using cID is running before your $.post is pleted. – Blazemonger Commented Dec 7, 2012 at 20:17
Add a ment  | 

2 Answers 2

Reset to default 7

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

You can store your whole data object instead of looping through and resetting the variable to a different value. Then you can access all your data outside. You should also define your variable outside of $.post so you have access to it

var cID;
$.post('URL', { }, function(data) {    
    cID = $.parseJSON(data);             
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744922585a4601224.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信