我写了一个名为的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函数出错。来人帮帮我!!提前致谢。
为什么你,如果你正在使用'XMLHttpRequest':那么你能如果没有的话,你可以对Ajax的单一功能例如使用
jquery
它,使用'jQuery'?你为什么不使用jQuery库中的jQuery.ajax http://api.jquery.com/jQuery.ajax/ –如果你要使用jQuery,你可以使用内置的'ajax()'函数。它使用起来非常简单,在代码开始时不必担心这个大问题。 http://api.jquery.com/jQuery.ajax/ –
As @ArunPJohny说为什么你使用XML请求与JQuery的? O.o –