ajax调用返回promis并通过调用函数将其解析为其值

ajax调用返回promis并通过调用函数将其解析为其值,ajax,promise,resolve,Ajax,Promise,Resolve,到现在为止,我读了大约6页的文章,其中包含文档和答案,但我不知道该怎么做 我的功能是在阅读了所有构建如下的东西之后: async function getFToken(postId){ const response = await $.ajax({ type: "POST", url: ajax_object.ajax_url, data:{ action:'get_f_token', postId: po

到现在为止,我读了大约6页的文章,其中包含文档和答案,但我不知道该怎么做

我的功能是在阅读了所有构建如下的东西之后:

async function getFToken(postId){
    const response = await $.ajax({
        type: "POST",
        url: ajax_object.ajax_url,
        data:{
        action:'get_f_token', 
        postId: postId,
        },
        success:function(response) {

        }
    });
    return response;
}
function getFeedback(postId){
    $(".show_company").hide();
    $(".show_feedback").show();
    $.ajax({
        type: "POST",
        dataType: "text json",
        url: ajax_object.ajax_url,
        data:{
        action:'get_feedback', 
        postId: postId,
        },
        success:function(response) {
            var postTitle = '';
            for (i in response) {
                postTitle += "<h1>" + response[i].post_title + "</h1><br/><br/>" + response[i].ID ;
                var test = getFToken(387);
                alert(Promise.resolve(test));
            };
            $("#result").html(postTitle);
        }
    });
}
在我的另一个函数中是这样的:

async function getFToken(postId){
    const response = await $.ajax({
        type: "POST",
        url: ajax_object.ajax_url,
        data:{
        action:'get_f_token', 
        postId: postId,
        },
        success:function(response) {

        }
    });
    return response;
}
function getFeedback(postId){
    $(".show_company").hide();
    $(".show_feedback").show();
    $.ajax({
        type: "POST",
        dataType: "text json",
        url: ajax_object.ajax_url,
        data:{
        action:'get_feedback', 
        postId: postId,
        },
        success:function(response) {
            var postTitle = '';
            for (i in response) {
                postTitle += "<h1>" + response[i].post_title + "</h1><br/><br/>" + response[i].ID ;
                var test = getFToken(387);
                alert(Promise.resolve(test));
            };
            $("#result").html(postTitle);
        }
    });
}

当您可以使用
async
/
wait
时,不要使用
success
回调:

async function getFToken(postId) {
    return $.ajax({
        type: "POST",
        url: ajax_object.ajax_url,
        data: {
            action: 'get_f_token', 
            postId: postId,
        }
    });
}

async function getFeedback(postId) {
    $(".show_company").hide();
    $(".show_feedback").show();
    const response = await $.ajax({
//                   ^^^^^
        type: "POST",
        dataType: "text json",
        url: ajax_object.ajax_url,
        data: {
            action: 'get_feedback', 
            postId: postId,
        }
    });
    let postTitle = '';
    for (const i in response) {
        postTitle += "<h1>" + response[i].post_title + "</h1><br/><br/>" + response[i].ID ;
        const test = await getFToken(387);
//                   ^^^^^
        alert(test); // no Promise.resolve, you don't want to alert a promise
    }
    $("#result").html(postTitle);
}
异步函数getFToken(postId){ 返回$.ajax({ 类型:“POST”, url:ajax\u object.ajax\u url, 数据:{ 操作:“获取令牌”, posted:posted, } }); } 异步函数getFeedback(postId){ $(“.show_company”).hide(); $(“.show_feedback”).show(); const response=wait$.ajax({ // ^^^^^ 类型:“POST”, 数据类型:“文本json”, url:ajax\u object.ajax\u url, 数据:{ 行动:“获取反馈”, posted:posted, } }); 让postTitle=''; for(const i响应){ postTitle+=“”+响应[i]。post_title+“

”+响应[i]。ID; 常数测试=等待getFToken(387); // ^^^^^ 警告(测试);//无承诺。解决,您不想警告承诺 } $(“#结果”).html(postTitle); }
在处理承诺时不要使用
success
回调。是
getFToken
是一个
异步函数,调用它会返回承诺。你需要像等待任何其他承诺一样等待它。你真的想在循环中调用
getFToken(387)
?@Bergi 1:我可以删除成功回调吗?2:我怎么等呢?3:这只是出于测试原因,之后它将与Loop处理的post中的实际post id进行切换。要使用
wait
,您当然需要使用
异步函数getFeedback
。然后您可以执行
var test=await getFToken(…);警报(测试)
getFToken(…)。然后(test=>{alert(test)})。有没有可能获取不同帖子的多个变量?在我的问题示例中,我用一个固定的数字(387)替换了传递变量的函数,但现在我想使用response[i].ID,如果我只是替换它,它只会给我一个变量,第二个变量为空。我猜这与wait函数有关?不,传递
response[i].ID
应该可以正常工作。你确定PHPAPI能正常工作吗?顺便说一句,如果
response
是一个数组!你说得对,非常感谢。php脚本中的类别应该是category_name=)