2012-09-17 93 views
-5

頁面中有一個鏈接,就像點擊鏈接時ajax會成功運行一樣,但是我怎麼做點擊這個鏈接的時候運行多個ajax調用並將相同的查詢字符串傳遞給不同的頁面?如何從同一個函數中進行多個ajax調用?

+1

只需將多個調用添加到點擊處理程序? – Endy

回答

0
var pages = ["page1.php","page2.php","folder/page3.php"] 
var requests = []; 

function multipleAjax(string) { 
    /*where string is "?value=somevalue&bool=true" or something*/ 

    for (i=0; i<pages.length;i++) { 
     requests[i] = getXMLHttpRequest(); 

     if (requests[i] != null) { 
      requests[i].open("Post", "/" + pages[i] + string); 

      requests[i].onreadystatechange = function() { 
       if (requests[i].readyState == 4) { 
        /*code to handle responseText returned*/ 
       }; 
      }; 

     }; 

     requests[i].send(); 
    }; 
}; 

function getXMLHttpRequest() { 
    var re = "nothing"; 
    try {re = new window.XMLHttpRequest();} catch(e1) {}; 
    try {re = new ActiveXObject("MSXML2.XMLHTTP.4.0");} catch(e) {}; 
    try {re = new ActiveXObject("Msxml2.XMLHTTP");} catch(e2) {}; 
    try {re = new ActiveXObject("Microsoft.XMLHTTP");} catch(e3) {}; 
    try {re = new ActiveXObject("MSXML2.XMLHTTP.3.0");} catch(ex) {}; 
    if (re != "nothing") {return re;} 
    else {return null;}; 
}; 
相關問題