ajax asyn true,jquery - AJAX ASYNC False vs. True - Stack Overflow

If you would like to implement asynchronous calls, then you want to organize all logic that is to be done with the resultant data into the .succcess() callback.

the reason that async is false is because when i fire the success callback the xml data isn't loaded but when I load a completed callback everything loads fine.

That line from your question is a bit confusing because the by definition, the success callback is executed once the request completed and any data returned is available.

This is an example of what you cannot do with async: true:

function ajaxrequest() {

var someValue;

$.get('serverFile.php', function(data){

someValue = data;

});

return someValue;

}

In that case, the variable someValue will be empty when it is returned because the callback you supplied isn't executed synchronously, or in-line, with the rest of your code.

To use async:true you can organize your code like this:

function getData() {

return $.get('serverFile.php');

}

function alertData() {

getData().done(function(data){

alert(data);

});

}

function logData() {

getData().done(function(data){

console.log(data);

});

}

I have broken it up this way so you can see that it can be helpful to isolate your request methods and have them return the jQuery Deferred object so that multiple other functions can utilize the same request if needed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值