JQuery – 嵌套的AJAX

我正在尝试使用以下代码执行嵌套的AJAX调用。 嵌套调用似乎不起作用。 我做错了吗?

$.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); } }); } }); 

您可能遇到线程类型问题,因为外部调用是异步的,并且可能在成功处理程序发挥作用之前超出范围。 尝试将成功呼叫分解为单独的function。

试试这个

 $.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); } }); } }); 

使用async/await语法。

 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))