我有這段代碼,如果 vID=0 則不起作用,因為 ajax.done 不是函式:
if (vID > 0) {
var ajax = CallPHP('GET', 'grid_main.php', "idquery=7&trkbid=" vID);
} else {
// Here I need a fake ajax so that the following code will work
var ajax=new Promise();
};
ajax.done(function(data, textStatus, jqXHR) {
// do something
}
我該如何解決?謝謝
uj5u.com熱心網友回復:
更合乎邏輯的是創建一個函式并呼叫它。
function nextStep(data, textStatus, jqXHR) {
// do something
}
if (vID > 0) {
var ajax = CallPHP('GET', 'grid_main.php', "idquery=7&trkbid=" vID).done(nextStep);
} else {
nextStep({});
};
你不能使用promise,因為jQuery 的Ajax 物件不是promise。它必須看起來像
var ajax = {
done: function(method) {
method({});
}
};
ajax.done(function(data, textStatus, jqXHR) {
console.log(data);
});
你可以使用 jQuery 的 deferred
var ajax = $.Deferred().resolve({});
ajax.done(function(data, textStatus, jqXHR) {
console.log(data);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/477952.html
標籤:javascript jQuery
上一篇:`this`是關鍵字還是文字?
