0

jqueryで同時ajax呼び出しを実行しようとしていますが、具体的なものは見つかりませんでした。2つのajax呼び出しを行うと、最初の呼び出しが終了した後にのみ、別の呼び出しが開始されます。試し$.ajaxSetUp{(async: true)}ましたが、思い通りに動作しませんでした。誰かがこれを手伝ってくれますか?

[編集済み] フォームがあり、フォームの送信時に、フォームと同じようにform.jsのajaxSubtmitを使用しますfile input

$("form").submit(function() {
  $(this).ajaxSubmit();
  $.getScript("url");
  return false; // to stop the normal working of form submit and submit form by ajax
})

ここで、2番目のajaxは、最初のajaxが終了した後にのみ呼び出されます。

4

2 に答える 2

1

それはあなたが言ったことであり、競合状態と呼ばれます。これを回避するには、各リクエストを格納する配列を作成するか、各リクエストを格納するローカル変数を作成するかの 2 つのオプションがあります。競合状態を回避する簡単な例を次に示します。

function customCallBack()
  {//    Ajax Status finished       http status OK
    if( (this.readyState == 4) && (this.status == 200) )
     {
       alert(this.responseText);
     }
  }

function anotherCallBack()
  {//    Ajax Status finished       http status OK
    if( (this.readyState == 4) && (this.status == 200) )
      {
        console.log(this.responseText);
      }
  }


function asyncPost(url, postArray, functionCallBack)
  {
    var request, query; //request must be a local var to avoid race condition
    query = '';
    for (i in postArray)//format post params
      {
        query += i + '=' + postArray[i] + '&';
      }
    try
      {//for modern browsers
        request = new XMLHttpRequest;
      }
    catch (err)
      {// legacy IE
        request = new ActiveXObject("Microsoft.XMLHTTP");
      }
       // your custom call back function
    request.onreadystatechange = functionCallBack;
    //            type   url  true for async call, false for sync call
    request.open("POST", url, true);

    //Header sent to indicate a post form
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    //making a post call
    request.send(query);
  };

//calling

asyncPost('yourUrl', {'field1' : 'value1', 'field2': 10}, customCallBack);

var post = new Array();
post['field1'] = 'value1';
post['field2'] = 10
//calling againg
asyncPost('anotherUrl', post, anotherCallBack);


// In jquery is like above, because you need the ajax call in a custom function
// and create a local var to hold your call:
function jqAjax(url)
  {
    var mycall  = $.get(url);
  }
于 2012-10-19T04:17:30.263 に答える
0

両方の Ajax 呼び出しは独立しています。最初の呼び出しが完了するのが遅れた場合、2 番目の呼び出しは独立して続行され、必要な数の ajax 呼び出しを行うことができます: 10、20、30 ...

メソッドを変更するには、以下の関数を使用します

function asyncCall(url, method, varArray, functionCallBack)
  {
    var request, query;
    query = '';
    for (i in postArray)//format params
      {
        query += i + '=' + postArray[i] + '&';
      }
    try
      {//for modern browsers
        request = new XMLHttpRequest;
      }
    catch (err)
      {// legacy IE
        request = new ActiveXObject("Microsoft.XMLHTTP");
      }
    request.onreadystatechange = functionCallBack;
    if(method == 'GET')
      {
        request.open("GET", url + '?' + query, true);
        request.send();
      }
    else
      {
        request.open("POST", url, true);
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.send(query);
      }
  };
console.log('start 1');
asyncCall('url1', 'GET', {foo : 'bar'}, function(){console.log('Call 1');});
console.log('start 2');
asyncCall('url2', 'POST', {foo : 'bar'}, function(){console.log('Call 2');});
console.log('start 3');
asyncCall('url3', 'POST', {foo : 'bar'}, function(){console.log('Call 3');});
于 2012-10-19T11:41:45.443 に答える