开发者

JQuery - Nested AJAX

开发者 https://www.devze.com 2022-12-25 07:43 出处:网络
I am trying to perform a nested AJAX call using the following code. The nested call doesn\'t seem to work. Am I doing anything wrong?

I am trying to perform a nested AJAX call using the following code. The nested call doesn't seem to work. Am I doing anything wrong?

$.ajax({
type: 'GET',
url: "/public/customcontroller/dosomething",
cache: false,
dataType: "html",
success: function(html_input)
{
    $.ajax({
        type: 'GET',
        url: "/public/customcontroller/getjobstatus",
        cache: false,
        dataType: "html",
        success: function(html_input){
        alert(html_input);
        }
    });   开发者_高级运维                                                                    
}
});


You could be having thread-type issues, given that the outside call is asynchronous and may go out of scope before the success handler can come into play. Try breaking the success call out into a separate function.


Try this

$.ajax({
type: 'GET',
url: "/public/customcontroller/dosomething",
cache: false,
dataType: "html",
async: false,
success: function(html_input)
{
    $.ajax({
        type: 'GET',
        url: "/public/customcontroller/getjobstatus",
        cache: false,
        dataType: "html",
        success: function(html_input){
        alert(html_input);
        }
    });                                                                       
}
});


Use the async/await syntax.

async function foo () {
    var html_input = await $.ajax({
        type: 'GET',
        url: "/public/customcontroller/dosomething",
        cache: false,
        dataType: "html"
    })

    var html_input2 = await $.ajax({
        type: 'GET',
        url: "/public/customcontroller/getjobstatus",
        cache: false,
        dataType: "html"
    })

    alert(html_input2)
}
foo().catch(e => console.log('some error:', e))
0

精彩评论

暂无评论...
验证码 换一张
取 消