标签:
我们从基础开始讲解Ajax:使用XMLHttpRequest 对象获得和显示来自服务器的数据。
step1:编写HTML页面01.HTML:
<!DOCTYPE html>
<html>
<head>
<title>My First Ajax Script</title>
<script src="script01.js"></script>
</head>
<body>
<p>
<!--/web-inf/在<a >标签中不识别,因此单独拿出来了 -->
<a id="makeTextRequest" href="gAddress.txt">Request a text file</a><br>
<a id="makeXMLRequest" href="us-states.xml">Request an XML file</a>
</p>
<div id="updateArea"> </div>
</body>
</html>
step2:编写Javascript文件script01.js:
/**
* 当加载页面时,会调用initAll()函数
*/
window.onload = initAll;
/**
* var xhr = false;
*脚本13-2 中的xhr 变量是在本章中会经常看到的一个变量。它是一个XMLHttpRequest 对象(或者说
*在初始化之后将成为XMLHttpRequest 对象)。目前,我们只需在任何函数之外创建它,使它成为全局变量。
*/
var xhr = false;
function initAll() {
document.getElementById("makeTextRequest").onclick = getNewFile;
document.getElementById("makeXMLRequest").onclick = getNewFile;
}
function getNewFile() {
makeRequest(this.href);
return false;
}
function makeRequest(url) {
/**
* 这些代码在makeRequest()中,这是有意思的地方。现代浏览器支持一个本机XMLHttpRequest 对
*象,这个对象是window 的一个属性。所以,我们检查这个属性是否存在,如果存在,就创建一个新的
*XMLHttpRequest 对象
*/
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else {
/**
* 虽然微软的IE(版本5.5 和版本6)支持XMLHttpRequest,
* 但是没有这个对象的本机版本。在这种情况下,必须检查浏览器是否支持ActiveX。
* 如果支持的话,就检查是否能够根据ActiveX 创建
*XMLHttpRequest 对象(使用try/catch 异常处理结构)。如果可以,就这么做。
*/
if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) { }
}
}
if (xhr) {
/**
* ? 设置xhr的onreadystatechange事件处理程序。每当xhr.readyState属性值发生变化时,
* 就会触发这个处理程序。调用open()并且传递3个参数:一个HTTP请求方法(例如"GET"、"POST"或"HEAD")、
* 服务器上一个文件的URL和一个布尔值,这个布尔值告诉服务器请求是否异步(也就是说,我们是否会等待请求完成)。
*最后,我们用send()发送刚才创建的请求。如果要请求POST,就传递这里给出的参数。
*/
xhr.onreadystatechange = showContents;
xhr.open("GET", url, true);
xhr.send(null);
}
else {
document.getElementById("updateArea").innerHTML = "Sorry, but I couldn‘t create an XMLHttpRequest";
}
}
function showContents() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
if (xhr.responseXML && xhr.responseXML.childNodes.length > 0) {
var outMsg = getText(xhr.responseXML.getElementsByTagName ("choices")[0]);
}
else {
var outMsg = xhr.responseText;
}
}
else {
var outMsg = "There was a problem with the request " + xhr.status;
}
document.getElementById("updateArea").innerHTML = outMsg;
}
}
function getText(inVal) {
if (inVal.textContent) {
return inVal.textContent;
}
return inVal.text;
}
step3:编写请求文本文件gAddress.text:
Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty,
?and dedicated to the proposition that all men are created equal.
Now we are engaged in a great civil war,testing whether that nation, or any nation so conceived and so dedicated,
?can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that
?field, as a final resting place for those who here gave their lives that that nation might live. It is altogether
?fitting and proper that we should do this.
But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The
?brave men,living and dead, who struggled here, have consecrated it, far above our poor power to add or detract.
?The world will little note, nor long remember what we say here,but it can never forget what they did here.
?It is for us the living, rather,to be dedicated here to the unfinished work which they who fought here have
?thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us
?-- that from these honored dead we take increased devotion to that cause for which they gave the last full
?measure of devotion – that we here highly resolve that these dead shall not have died in vain -- that this
?nation, under God, shall have a new birth of freedom -- and that government of the people, by the people,
?for the people,shall not perish from the earth.
step4:编写xml文件us_states.xml:
<?xml version="1.0" encoding="UTF-8"?>
<choices xml:lang="EN">
<item><label>Alabama</label><value>AL</value></item>
<item><label>Alaska</label><value>AK</value></item>
<item><label>Arizona</label><value>AZ</value></item>
<item><label>Arkansas</label><value>AR</value></item>
<item><label>California</label><value>CA</value></item>
<item><label>Colorado</label><value>CO</value></item>
<item><label>Connecticut</label><value>CT</value></item>
<item><label>Delaware</label><value>DE</value></item>
<item><label>Florida</label><value>FL</value></item>
<item><label>Georgia</label><value>GA</value></item>
<item><label>Hawaii</label><value>HI</value></item>
<item><label>Idaho</label><value>ID</value></item>
<item><label>Illinois</label><value>IL</value></item>
<item><label>Indiana</label><value>IN</value></item>
<item><label>Iowa</label><value>IA</value></item>
<item><label>Kansas</label><value>KS</value></item>
<item><label>Kentucky</label><value>KY</value></item>
<item><label>Louisiana</label><value>LA</value></item>
<item><label>Maine</label><value>ME</value></item>
<item><label>Maryland</label><value>MD</value></item>
<item><label>Massachusetts</label><value>MA</value></item>
<item><label>Michigan</label><value>MI</value></item>
<item><label>Minnesota</label><value>MN</value></item>
<item><label>Mississippi</label><value>MS</value></item>
<item><label>Missouri</label><value>MO</value></item>
<item><label>Montana</label><value>MT</value></item>
<item><label>Nebraska</label><value>NE</value></item>
<item><label>Nevada</label><value>NV</value></item>
<item><label>New Hampshire</label><value>NH</value></item>
<item><label>New Jersey</label><value>NJ</value></item>
<item><label>New Mexico</label><value>NM</value></item>
<item><label>New York</label><value>NY</value></item>
<item><label>North Carolina</label><value>NC</value></item>
<item><label>North Dakota</label><value>ND</value></item>
<item><label>Ohio</label><value>OH</value></item>
<item><label>Oklahoma</label><value>OK</value></item>
<item><label>Oregon</label><value>OR</value></item>
<item><label>Pennsylvania</label><value>PA</value></item>
<item><label>Rhode Island</label><value>RI</value></item>
<item><label>South Carolina</label><value>SC</value></item>
<item><label>South Dakota</label><value>SD</value></item>
<item><label>Tennessee</label><value>TN</value></item>
<item><label>Texas</label><value>TX</value></item>
<item><label>Utah</label><value>UT</value></item>
<item><label>Vermont</label><value>VT</value></item>
<item><label>Virginia</label><value>VA</value></item>
<item><label>Washington</label><value>WA</value></item>
<item><label>West Virginia</label><value>WV</value></item>
<item><label>Wisconsin</label><value>WI</value></item>
<item><label>Wyoming</label><value>WY</value></item>
</choices>
标签:
原文地址:http://www.cnblogs.com/CooderIsCool/p/4736212.html