2013-02-01 33 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.