多个链式jQuery AJAX POST webservice调用

时间:2021-05-27 14:29:53

Im using jQuery AJAX Calls to a webservice. The calls have to be chained. Based on the solution of https://*.com/a/995648/296575 I created an AJAX queue.

我使用jQuery AJAX调用web服务。电话必须链接。基于https://*.com/a/995648/296575的解决方案,我创建了一个AJAX队列。

function ajaxQueue(step) {
          switch(step) {
            case 0: $.ajax({
                      type: "POST",
                      url: "url",
                      data: SoapRequest1,
                      contentType: "text/xml",
                      complete: storeData1                          
                     }); break;
            case 1: $.ajax({
                      type: "POST",
                      url: "url",
                      data: SoapRequest2,
                      contentType: "text/xml",
                      complete: storeData2
                    }); break;
            case 2: $.ajax({
                      type: "POST",
                      url: "url",
                      data: SoapRequest3,
                      contentType: "text/xml",
                      complete: storeData3
                    }); break;
          }
        }       
        //start ajaxQueue
        ajaxQueue(0);

        function storeData1(xmlHttpRequest, status) 
        {                                           
                updateData1(xmlHttpRequest.responseXML);
                ajaxQueue(1);           
        }

        function storeData2(xmlHttpRequest, status) 
        {
              updateData2(xmlHttpRequest.responseXML);
                ajaxQueue(2);
      }

        function storeData3(xmlHttpRequest, status) 
        {
                updateData3(xmlHttpRequest.responseXML);

      }

Now I have the following issue: If the function is executed, only the first case returns the correct XML from the webservice. The second and third calls lead to an error. (parseerror, data is null).

现在我遇到以下问题:如果执行该函数,则只有第一种情况从Web服务返回正确的XML。第二次和第三次调用导致错误。 (parseerror,data为null)。

The calls are cross domain, which is supressed by:

这些电话是跨域的,受到以下因素的压制:

 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");

If I add async:false to each call, all of them are executed correctly. If I change the order of the calls, always the first one is executed correctly.

如果我为每个调用添加async:false,则所有调用都会正确执行。如果我更改了调用的顺序,则始终会正确执行第一个调用。

Can anyone help me with this one? Or tell me If you need more information.

任何人都可以帮我这个吗?或者告诉我如果您需要更多信息。

Thank you!

2 个解决方案

#1


0  

hmm, if I was chaining ajax calls, I would just put the second call inside of the first calls success handler or some variation of that.

嗯,如果我链接ajax调用,我会把第二个调用放在第一个调用成功处理程序或其中的一些变体。

$.ajax( {
   url: 'blah.com',
   success: function( result ) {
      $.ajax( {
         url: 'blah.com',
         success: function( result ) {
            // now in second leg of the chain.
            // you can keep going like this forever.

         }
      } );
   }
} );

#2


0  

Use callbacks (something to call when the AJAX request has finished), pass it as an argument into your queue:

使用回调(在AJAX请求完成时调用的东西),将其作为参数传递到队列中:

function ajaxQueue(step, callback) {
      switch(step) {
        case 0: $.ajax({
                  type: "POST",
                  url: "url",
                  data: SoapRequest1,
                  contentType: "text/xml",
                  complete: callback                          
                 }); break;
        case 1: $.ajax({
                  type: "POST",
                  url: "url",
                  data: SoapRequest2,
                  contentType: "text/xml",
                  complete: callback
                }); break;
        case 2: $.ajax({
                  type: "POST",
                  url: "url",
                  data: SoapRequest3,
                  contentType: "text/xml",
                  complete: callback
                }); break;
      }
    }       

    //start ajaxQueue
    ajaxQueue(0, function() {
        // do something when ajaxQueue(0) has returned from AJAX call
    });

    function storeData1(xmlHttpRequest, status) 
    {          
         // passes updateData1 as a callback, automatically passes params from AJAX call                                                 
         ajaxQueue(1, updateData1);           
    }

#1


0  

hmm, if I was chaining ajax calls, I would just put the second call inside of the first calls success handler or some variation of that.

嗯,如果我链接ajax调用,我会把第二个调用放在第一个调用成功处理程序或其中的一些变体。

$.ajax( {
   url: 'blah.com',
   success: function( result ) {
      $.ajax( {
         url: 'blah.com',
         success: function( result ) {
            // now in second leg of the chain.
            // you can keep going like this forever.

         }
      } );
   }
} );

#2


0  

Use callbacks (something to call when the AJAX request has finished), pass it as an argument into your queue:

使用回调(在AJAX请求完成时调用的东西),将其作为参数传递到队列中:

function ajaxQueue(step, callback) {
      switch(step) {
        case 0: $.ajax({
                  type: "POST",
                  url: "url",
                  data: SoapRequest1,
                  contentType: "text/xml",
                  complete: callback                          
                 }); break;
        case 1: $.ajax({
                  type: "POST",
                  url: "url",
                  data: SoapRequest2,
                  contentType: "text/xml",
                  complete: callback
                }); break;
        case 2: $.ajax({
                  type: "POST",
                  url: "url",
                  data: SoapRequest3,
                  contentType: "text/xml",
                  complete: callback
                }); break;
      }
    }       

    //start ajaxQueue
    ajaxQueue(0, function() {
        // do something when ajaxQueue(0) has returned from AJAX call
    });

    function storeData1(xmlHttpRequest, status) 
    {          
         // passes updateData1 as a callback, automatically passes params from AJAX call                                                 
         ajaxQueue(1, updateData1);           
    }