将async:false AJAX函数转换为$.Deferred函数



我两天来一直在努力将一个简单的带有async:false的AJAX函数转换为一个漂亮的jQuery Deferred((函数。我试图返回AJAX响应的所有内容都不起作用:-(

这是我的旧(短(版本代码:

function uiText($textID) {
var text = $.ajax({
url: "uitext.php",
async: false,
success: function(response){}
};
return text.response;
}
console.log(uiText('foo'));  //shows me the response from uitext.php

如何使用$.Deferred和/或$.when((.done((

Thx Mike

$.Deferred和/或$.when((.done((

不要。这已经过时了。$.ajax返回一个promise兼容对象。

function uiText($textID) {
const request = $.ajax({
url: "uitext.php"
});
return request;
}
uiText('foo').then( response => console.log(response) );

最新更新