简体   繁体   English

Javascript ajax 同步请求不起作用

[英]Javascript ajax synchronous request is not working

I'm making sequential AJAX requests for my websites.我正在为我的网站发出连续的 AJAX 请求。 There are 2 POST requests.有 2 个 POST 请求。 The second request should be processed after first request is done.第二个请求应该在第一个请求完成后处理。 My code is as below:我的代码如下:

$.ajax({
    type: 'POST',
    url: '/backend/edge/enableNewAgent/',
    async: false,
    success: function () {
        console.log("First Process Done");
    }
});
$.ajax({
    type: 'POST',
    url: '/backend/edge/deleteOldAgent/',
    async: false,
    success: function () {
        console.log("Second Process Done");
    }
});

The second process is done after first process, but the console logging is executed after second process done, not after first process done.第二个进程在第一个进程之后完成,但是控制台日志是在第二个进程完成后执行的,而不是在第一个进程完成之后。 I want the console.log is executed soon after first process done, then continue executing the second process.我希望在第一个进程完成后立即执行console.log ,然后继续执行第二个进程。 Can someone help ?有人可以帮忙吗?

Using async: false means that you never yield to the event loop, and the console.log lines get queued up (as do all other display updates).使用async: false意味着您永远不会屈服于事件循环,并且console.log行会排队(所有其他显示更新也是如此)。

My approach would be this:我的方法是这样的:

function enableNewAgent() {
    return $.post('/backend/edge/enableNewAgent/',
        () => console.log('enableNewAgent Done')
    );
}

function deleteOldAgent() {
    return $.post('/backend/edge/deleteOldAgent/',
        () => console.log('deleteOldAgent Done')
    );
}

enableNewAgent().then(deleteOldAgent);

If you require further operations, add them to the .then chain:如果您需要进一步的操作,请将它们添加到.then链中:

enableNewAgent().then(deleteOldAgent).then(nextOperation);

If you want to write synchronous "looking" code and avoid synchronous XMLHttpRequest - you can use async/await如果您想编写同步的“外观”代码并避免同步 XMLHttpRequest - 您可以使用 async/await

async function doAjax() {
    await $.ajax({
        type: 'POST',
        url: '/backend/edge/enableNewAgent/',
        success: function() {
            console.log("First Process Done");
        }
    });
    await $.ajax({
        type: 'POST',
        url: '/backend/edge/deleteOldAgent/',
        success: function() {
            console.log("Second Process Done");
        }
    });
}

actually, it's better done like实际上,最好这样做

async function doAjax() {
    await $.ajax({
        type: 'POST',
        url: '/backend/edge/enableNewAgent/'
    });
    console.log("First Process Done");
    await $.ajax({
        type: 'POST',
        url: '/backend/edge/deleteOldAgent/'
    });
    console.log("Second Process Done");
}

Note, it HAS to be done inside a function (doesn't have to be a separate function like this, just put that there to enforce the idea ... await is only inside async function)请注意,它必须在一个函数内完成(不必像这样是一个单独的函数,只需将其放在那里以强制执行这个想法...... await仅在async函数内)

You can try你可以试试

$.ajax({
  type: 'POST',
  url: '/backend/edge/enableNewAgent/',
  success: function() {
    console.log("First Process Done");
  }
}).then(function(){
      $.ajax({
      type: 'POST',
      url: '/backend/edge/deleteOldAgent/',
      success: function() {
        console.log("Second Process Done");
      }
    });

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

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