2012-05-18 44 views
0

我正在研究那些我(可惜)不能使用jQuery的項目。我需要在jQuery中做一些簡單的事情,但我無法在純JavaScript中做到這一點。所以,我需要使用另一個響應表單運行一個ajax請求。在jQuery的它看起來像:在另一個之後運行一個ajax

$.get("date.php", "", function(data) { 
    var date=data; 
    $("#date").load("doku.php?id="+date.replace(" ", "_")+" #to_display", function() { 
    $(document.createElement("strong")).html("<a href=\"doku.php?id="+date.replace(" ", "_")+"\">"+date+"</a>:").prependTo($(this)); 
    }); 

}); 

這是我的純JS代碼,這是不工作:

if (window.XMLHttpRequest) 
    { 
    ObiektXMLHttp = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) 
    { 
    ObiektXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    if(ObiektXMLHttp) 
    { 
    ObiektXMLHttp.open("GET", "date.php"); 

    ObiektXMLHttp.onreadystatechange = function() 
    { 

     if (ObiektXMLHttp.readyState == 4) 
     { 
     var date = ObiektXMLHttp.responseText; 
     if (window.XMLHttpRequest) 
     { 
      ObiektXMLHttp = new XMLHttpRequest(); 
     } else if (window.ActiveXObject) 
     { 
      ObiektXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 

     ObiektXMLHttp.open("GET", "doku.php?id="+date.replace(" ", "_")); 
     ObiektXMLHttp.onreadystatechange = function() 
     { 
      if (ObiektXMLHttp.readyState == 4) 
      { 
      alert(ObiektXMLHttp.responseText); 
      } 
     } 
     } 
    } 
    ObiektXMLHttp.send(null); 
    } 

我在做什麼撥錯?

+1

定義「不工作」。你是否遇到錯誤,或者看不到代碼的影響,或者是什麼? –

+2

我知道這是脫離主題,但我想至少提到,如果你不能使用jQuery的原因是由於與另一個庫的衝突,你應該檢查jQuery.noConflict(),這使得這完全可能。 –

回答

1

你忘了打電話給ObiektXMLHttp.send(null);在第二種情況:

//.... 
ObiektXMLHttp.open("GET", "doku.php?id="+date.replace(" ", "_")); 
ObiektXMLHttp.onreadystatechange = function() { 
    if (ObiektXMLHttp.readyState == 4) 
    { 
     alert(ObiektXMLHttp.responseText); 
    } 
}; 
//Here 
ObiektXMLHttp.send(null); 
+0

哦,是的,你是對的。我完全忘了它。感謝您的幫助。 – ghi

0

目前尚不清楚你有什麼錯(?你可以告訴我們的),但我建議要依靠這樣的一些輔助功能:

function xhrGet(url, callback) { 
    if (window.XMLHttpRequest) 
     var xhr = new XMLHttpRequest(); 
    else if (window.ActiveXObject) 
     var xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
    if (!xhr) return; 
    xhr.open("GET", url); 
    xhr.onreadystatechange = function() { 
     if (xhr.readyState !== 4) return; 
     if (typeof callback === "function") callback(xhr); 
    }; 
    xhr.send(null); 
    return xhr; 
} 

因此,所有你需要做的就是使用此功能:

xhrGet("date.php", function(x1) { 
    xhrGet("doku.php?id=" + date.replace(" ", "_"), function(x2) { 
     // do stuff 
     // x1 and x2 are respectively the XHR object of the two requests 
    }); 
}); 
1

如何像這樣(天真的原型):

// xhr object def 
var xhr = { 
    obj: function() { 
    if (window.XMLHttpRequest) { 
     return new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { 
     return new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    throw new Error("can't init xhr object"); 
    }, 
    get: function(url, fn) { 
    var xhr = this.obj(); 
    xhr.onreadystatechange = function() { 
     if (xhr.readyState == 4) { 
     fn(xhr.responseText); 
     } 
    }; 
    xhr.open("GET", url); 
    xhr.send(null); 
    } 
}; 

// implementation 
xhr.get("date.php", function(data){ 
    xhr.get("doku.php?id=" + data.replace(" ", "_"), function(data){ 
    alert(data); 
    }); 
}); 
相關問題