2013-02-01 64 views
2

我寫了一個名爲的getContent()和結構化這樣使用Ajax功能的問題

的getContent(whichcontent)的AJAX功能{//此處代碼來獲取內容}

具體的代碼是在這裏:

function getXmlHttpRequestObject() { 
    if (window.XMLHttpRequest) { 
    return new XMLHttpRequest(); //Not IE 
    } else if(window.ActiveXObject) { 
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE 
    } else { 
    alert("Your browser doesn't support the XmlHttpRequest object. Better upgrade to Firefox."); 
    } 
} 

var receiveReq = getXmlHttpRequestObject(); 

var page_id = 1; 

function getContent(which_page,append){ 
    if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {  

    receiveReq.open("GET", 'spt/page_'+which_page, true);//get the text file 

    receiveReq.onreadystatechange = function(){ 
     handleGetContent(which_page,append); 
    } 
    receiveReq.send(null); 
    }  
} 

function handleGetContent(which_page,append){ 
    if (receiveReq.readyState == 4) {   
     if(append == 1){ 
      $('#container').append("<div class='page' id='page_"+which_page+"'><div class='title'>圍城</div><p>" + receiveReq.responseText + "</p><div class='pagenum'>"+which_page+"</div></div>"); 

     } 
     if(append == 0){ 
      $('#container').prepend("<div class='page' id='page_"+which_page+"'><div class='title'>圍城</div><p>" + receiveReq.responseText + "</p><div class='pagenum'>"+which_page+"</div></div>");   
     } 
    } 
} 

而且我用的getContent這樣

$(document).ready(function(){ 
    getContent(1,1); 
    getContent(2,1); 
} 

問題是我只得到Ø ne ...而另一個id爲page_2的則沒有出現。我想知道ajax函數是否只能在js函數中調用一次,或者我只是讓ajax函數出錯。來人幫幫我!!提前致謝。

+5

爲什麼你,如果你正在使用'XMLHttpRequest':那麼你能如果沒有的話,你可以對Ajax的單一功能例如使用jquery它,使用'jQuery'?你爲什麼不使用jQuery庫中的jQuery.ajax http://api.jquery.com/jQuery.ajax/ –

+0

如果你要使用jQuery,你可以使用內置的'ajax()'函數。它使用起來非常簡單,在代碼開始時不必擔心這個大問題。 http://api.jquery.com/jQuery.ajax/ –

+0

As @ArunPJohny說爲什麼你使用XML請求與JQuery的? O.o –

回答

0

我覺得代碼是好,但如果你使用Ajax有疑問http://www.w3schools.com/ajax/ajax_aspphp.asp

或者試試這個代碼

// change the function like 
function getContent(which_page,append){ 
    var receiveReq = getXmlHttpRequestObject();// Create object here 

    receiveReq.open("GET", 'spt/page_'+which_page+'.txt', true);//get the text file 
    //write the extension of the file 
    receiveReq.onreadystatechange = function(){ 
     if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {  
      handleGetContent(receiveReq,which_page,append); 
     } 

    }  
    receiveReq.send(null); 
} 
// add an extra parameter in this function 
function handleGetContent(receiveReq,which_page,append){ 
    if (receiveReq.readyState == 4) {   
     if(append == 1){ 
      $('#container').append("<div class='page' id='page_"+which_page+"'><div class='title'>圍城</div><p>" + receiveReq.responseText + "</p><div class='pagenum'>"+which_page+"</div></div>"); 

     } 
     if(append == 0){ 
      $('#container').prepend("<div class='page' id='page_"+which_page+"'><div class='title'>圍城</div><p>" + receiveReq.responseText + "</p><div class='pagenum'>"+which_page+"</div></div>");   
     } 
    } 
} 
$(document).ready(function(){ 
    getContent(1,1); 
    getContent(2,1); 
});// change this line also 
+0

這確實有效。非常感謝!! :) – Liang

0

XMLHttpRequest的調用是asyncronous,所以如果你只使用一個請求對象getContent第二通話將被忽略,因爲receiveReq.readyState既不是0也不4.