简体   繁体   English

嵌套的AJAX调用成功函数无法运行

[英]Nested AJAX call success function does not get run

I have the following ajax call nested inside another, but after the first one gets run, nothing gets logged to the console meanwhile it should. 我将以下ajax调用嵌套在另一个调用中,但是在第一个调用运行之后,没有东西同时应记录到控制台。 Where is the issue? 问题出在哪里?

$.ajax({
    url: "https://ck:8081/get-username",
    type: "get",
    success: function (response) {
        userName = response
        $.ajax({
            url: "https://ck:8081/new-chatuser?username=" + userName,
            type: "get",
            success: function (response) {
                console.log('response' + response)
                $('onlineMembers').append(response)
            }
        })
    }
})

There is most likely a failure in one of the requests you are sending, to remedie to that, you need to handle the errors as described in the code below : 为了解决这个问题,您发送的请求之一很可能会失败,因此,您需要按照以下代码中的描述处理错误:

$.ajax({
    url: 'https://ck:8081/get-username',
    type: 'get',
    success: function(response1) {
        console.log(`First succeeded : ${response2.toString()}`);
        $.ajax({
            url: 'https://ck:8081/new-chatuser?username=' + response1,
            type: 'get',
            success: function (response2) {
                console.log(`Second succeeded : ${response2.toString()}`);
                $('onlineMembers').append(response2);
            },
            error: function(error) {
                console.log(`Second failed : ${error.toString()}`);
            }
        });
    },
    error: function(error) {
        console.log(`First failed : ${error.toString()}`);
    }
});

You may want to change $('onlineMembers') because I don't think the selector you are using is correct. 您可能要更改$('onlineMembers')因为我认为您使用的选择器不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM