ajax获得文件内容,Ajax获得站点文件内容实例
一个简单的Ajax实例:选择一部著作,会通过 Ajax 实时获得相关的名字。
把4个html文件放到 web站点 的同一个文件下。
index.html
一个简单的不涉及服务器的Ajax实例// 声明一个引用 XMLHttpRequest 的变量
var xhr = null;
// 选择一个著作时调用的函数
function updateCharacters() {
var selectShow = document.getElementById("selShow").value;
if (selectShow == "") {
document.getElementById("divCharacters").innerHTML = "";
return ;
}
// 实例化一个 XMLHttpRequest 对象
if (window.XMLHttpRequest) {
// 非IE
xhr = new XMLHttpRequest();
} else {
// IE
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.onreadystatechange = callbackHandler;
url = selectShow + ".html";
xhr.open("post", url, true);
xhr.send(null);
}
// this is the function that will repeatedly be called by our
// XMLHttpRequest object during the life cycle of request
function callbackHandler() {
if (xhr.readyState == 4) {
document.getElementById("divCharacters").innerHTML = xhr.responseText;
}
}
我们的第一个Ajax实例
选择一个名著:
西游记
红楼梦
水浒传
三国演义
返回,名著中主要任务:
xyj.html
唐僧
孙悟空
猪八戒
唐僧
观音姐姐
西天如来
hlm.html
贾宝玉
林黛玉
薛宝钗
shz.html
林冲
李逵
宋江
时迁
sgyy.html
刘备
关羽
张飞
曹操
小乔
诸葛亮
一个简单的Ajax实例:选择一部著作,会通过 Ajax 实时获得相关的名字。 把4个html文件放到 web站点 的同一个文件下。 index.html一个简单的不涉及服务器的Ajax实例 // 声明一个引用 XMLHttpRequest 的变量 var xhr = null; // 选择一个著作时调用的函数 function updateCharacters() { var selectShow = document.getElementById("selShow").value; if (selectShow == "") { document.getElementById("divCharacters").innerHTML = ""; return ; } // 实例化一个 XMLHttpRequest 对象 if (window.XMLHttpRequest) { // 非IE xhr = new XMLHttpRequest(); } else { // IE xhr = new ActiveXObject("Microsoft.XMLHTTP"); } xhr.onreadystatechange = callbackHandler; url = selectShow + ".html"; xhr.open("post", url, true); xhr.send(null); } // this is the function that will repeatedly be called by our // XMLHttpRequest object during the life cycle of request function callbackHandler() { if (xhr.readyState == 4) { document.getElementById("divCharacters").innerHTML = xhr.responseText; } } 我们的第一个Ajax实例 选择一个名著: 西游记 红楼梦 水浒传 三国演义 返回,名著中主要任务: xyj.html 唐僧 孙悟空 猪八戒 唐僧 观音姐姐 西天如来 hlm.html 贾宝玉 林黛玉 薛宝钗 shz.html 林冲 李逵 宋江 时迁 sgyy.html 刘备 关羽 张飞 曹操 小乔 诸葛亮