添加async和wait关键字后,ajax函数不起作用



下面提到的代码运行良好。

$.ajax({
url: '?module=abc&action=xyz',
//Ajax events
beforeSend: function (e) {
//
},
success: function (e) {
const URL1 = '?module=mno&action=pqr';
fetch(URL1);
},
error: function (e) {
alert('error ' + e.message);
},
// Form data
data: formData,
type: 'POST',
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});

但如果我添加asyncawait,它就不起作用,而且控制台中也不会出现任何错误。类似以下代码:

$.ajax({
url: '?module=abc&action=xyz',
//Ajax events
beforeSend: function (e) {
//
},
success: async function (e) {
const URL1 = '?module=mno&action=pqr';
const RES = await fetch(URL1);
let output = await RES.json();
},
error: function (e) {
alert('error ' + e.message);
},
// Form data
data: formData,
type: 'POST',
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});

不知道我哪里错了。

好的,明白了,我刚刚从success内部删除了代码,并添加了一个新的function,它就工作了。

$.ajax({
url: '?module=abc&action=xyz',
//Ajax events
beforeSend: function (e) {
//
},
success: async function (e) {
//I added the below 'runquery()' function.
runquery();
},
error: function (e) {
alert('error ' + e.message);
},
// Form data
data: formData,
type: 'POST',
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});

然后添加了一个新的function

async function runquery() {   
const URL1 = '?module=mno&action=pqr';
const RES = await fetch(URL1);
let output = await RES.json();
}

最新更新