您的位置:首页 > 其它

使用Ajax技术从本地取回xml文件信息

2016-03-21 11:09 375 查看
//1.
编写note.xml

<note>

<to>Mr.Dylan</to>

<body>Don't forget the meeting!</body>

</note>

2.编写getxml.html

[html] view
plain copy

<html>

<head>

<script type="text/javascript">

var xmlhttp;

function loadXMLDoc(url)

{

xmlhttp=null;

if (window.XMLHttpRequest)

{// code for IE7, Firefox, Opera, etc.

xmlhttp=new XMLHttpRequest();

}

else if (window.ActiveXObject)

{// code for IE6, IE5

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

}

if (xmlhttp!=null)

{

xmlhttp.onreadystatechange=state_Change;

xmlhttp.open("GET",url,true);

xmlhttp.send(null);

}

else

{

alert("Your browser does not support XMLHTTP.");

}

}

function state_Change()

{

if (xmlhttp.readyState==4)

{// 4 = "loaded"

<span style="color:#FF0000;">if (xmlhttp.status==200 || xmlhttp.status==0)</span>

{// 200 = "OK"

document.getElementById('A1').innerHTML=xmlhttp.status;

document.getElementById('A2').innerHTML=xmlhttp.statusText;

document.getElementById('A3').innerHTML=xmlhttp.responseText;

}

if(xmlhttp.status==404)

{

alert("Page not found!");

}

else

{

alert("Problem retrieving XML data:" + xmlhttp.statusText);

}

}

}

</script>

</head>

<body>

<h2>Using the HttpRequest Object</h2>

<p><b>Status:</b>

<span id="A1"></span>

</p>

<p><b>Status text:</b>

<span id="A2"></span>

</p>

<p><b>Response:</b>

<br /><span id="A3"></span>

</p>

<button onclick="loadXMLDoc('note.xml')">Get XML</button>

</body>

</html>

注:红色部分,由于获取的是本地的XML所以此处浏览器返回的xmlhttp.status=0.

也就是说没有通过Web服务器形式的Ajax请求返回值都是0;


3. 使用火狐打开getxml.html:



-------------------------

present by dylan.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: