简体   繁体   English

jQuery 中嵌套 ajax 调用的问题

[英]Issue with nested ajax call in jQuery

I have a situation where I make an ajax call, on returning from which (successfully), I make an another ajax call with the returned data and submit the page.我有一种情况,我进行了 ajax 调用,在返回时(成功),我使用返回的数据进行了另一个 ajax 调用并提交了页面。 The pseudo code looks something like this:伪代码如下所示:

$.ajax({
    url: 'first_page.jsp',
    type: 'POST',
    dataType: 'JSON',
    data: {...}
}).done(function(response) {
    $.ajax({
        url: '2nd_page.jsp',
        type: 'POST',
        dataType: 'JSON',
        data: {...}
    });
    thisPage.submit();
});

The inner ajax call is not executed if I do not comment out the 'submit' line.如果我不注释掉“提交”行,则不会执行内部 ajax 调用。 I do not understand the behaviour.我不明白这种行为。 Can someone please help me with this.有人可以帮我解决这个问题。 Thanks谢谢

You need to execute submit on second ajax sucess.您需要在第二次ajax成功时执行提交。

For example例如

    $.ajax({
            url: '',
            type: 'GET',
            data: {

            },
            success: function (data) {
    $.ajax({
            url: '',
            type: 'GET',
            data: {

            },
            success: function (data) {
    //do your stuff
}
     $("input[type='submit']").click(function(event){
        event.preventDefault();
         $.ajax({
              url:'abcd.php',
              method:'post',
              type:'json',
              async:false,
              success:function(response){
              $.ajax({
              url:'abcde.php',
              method:'post',
              type:'json',
              data:{res:response},
              success:function(response){
              console.log(response);
              }
              });
              $("form").submit();   
              }

         });
         });

Try this one.Its working correctly.试试这个。它工作正常。

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

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