用Ajax讀取XML格式的數(shù)據(jù)
更新時(shí)間:2006年11月06日 00:00:00 作者:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Ajax Hello World</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function startRequest(){
createXMLHttpRequest();
try{
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "data.xml", true);
xmlHttp.send(null);
}catch(exception){
alert("您要訪問的資源不存在!");
}
}
function handleStateChange(){
if(xmlHttp.readyState == 4){
if (xmlHttp.status == 200 || xmlHttp.status == 0){
// 取得XML的DOM對(duì)象
var xmlDOM = xmlHttp.responseXML;
// 取得XML文檔的根
var root = xmlDOM.documentElement;
try
{
// 取得<info>結(jié)果
var info = root.getElementsByTagName('info');
// 顯示返回結(jié)果
alert("responseXML's value: " + info[0].firstChild.data);
}catch(exception)
{
}
}
}
}
</script>
</head>
<body>
<div>
<input type="button" value="return ajax responseXML's value"
onclick="startRequest();" />
</div>
</body>
</html>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Ajax Hello World</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function startRequest(){
createXMLHttpRequest();
try{
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "data.xml", true);
xmlHttp.send(null);
}catch(exception){
alert("您要訪問的資源不存在!");
}
}
function handleStateChange(){
if(xmlHttp.readyState == 4){
if (xmlHttp.status == 200 || xmlHttp.status == 0){
// 取得XML的DOM對(duì)象
var xmlDOM = xmlHttp.responseXML;
// 取得XML文檔的根
var root = xmlDOM.documentElement;
try
{
// 取得<info>結(jié)果
var info = root.getElementsByTagName('info');
// 顯示返回結(jié)果
alert("responseXML's value: " + info[0].firstChild.data);
}catch(exception)
{
}
}
}
}
</script>
</head>
<body>
<div>
<input type="button" value="return ajax responseXML's value"
onclick="startRequest();" />
</div>
</body>
</html>
server.xml
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="GB2312" ?>
<root>
<info>hello world!</info>
</root>
您可能感興趣的文章:
- AJAX使用post發(fā)送數(shù)據(jù)xml格式接受數(shù)據(jù)
- 用Ajax讀取xml文件的簡(jiǎn)單例子
- jQuery+ajax讀取并解析XML文件的方法
- Jquery Ajax學(xué)習(xí)實(shí)例 向頁(yè)面發(fā)出請(qǐng)求,返回XML格式數(shù)據(jù)
- javascript解析ajax返回的xml和json格式數(shù)據(jù)實(shí)例詳解
- jquery $.ajax()取xml數(shù)據(jù)的小問題解決方法
- JS通過ajax動(dòng)態(tài)讀取xml文件內(nèi)容的方法
- 通過AJAX的JS、JQuery兩種方式解析XML示例介紹
- JS使用ajax從xml文件動(dòng)態(tài)獲取數(shù)據(jù)顯示的方法
- 用JQuery 實(shí)現(xiàn)AJAX加載XML并解析的腳本
- Ajax對(duì)xml信息的接收和處理操作實(shí)例分析
相關(guān)文章
js統(tǒng)計(jì)網(wǎng)頁(yè)在線時(shí)間的腳本
分析用戶行為時(shí),某網(wǎng)頁(yè)停留時(shí)間可能會(huì)是個(gè)可參考的數(shù)據(jù) 由后臺(tái)分析函數(shù)取一個(gè)合理值作為在線時(shí)間2008-04-04JQuery ajax中error返回錯(cuò)誤及一直返回error的解答
本文由腳本之家小編給大家分享有關(guān) JQuery ajax中error返回錯(cuò)誤及一直返回error的解答總結(jié),需要的朋友可以參考下2015-09-09使用ajax技術(shù)無(wú)刷新動(dòng)態(tài)調(diào)用股票信息
新浪的財(cái)金頻道一直感覺做得很好。但由于最近網(wǎng)速慢的緣故,查看股票信息時(shí)網(wǎng)頁(yè)老是打不開。這幾天一直在研究ajax,于是用jquery自己做了一個(gè)自動(dòng)讀取新浪股票實(shí)時(shí)數(shù)據(jù)的頁(yè)面。2008-11-11Axios和Ajax的區(qū)別是什么(詳細(xì)介紹)
ajax技術(shù)實(shí)現(xiàn)了局部數(shù)據(jù)的刷新,axios實(shí)現(xiàn)了對(duì)ajax的封裝,axios有的ajax都有,ajax有的axios不一定有,總結(jié)一句話就是axios是ajax,ajax不止axios,本文對(duì)Axios和Ajax的區(qū)別是什么給大家講解的非常詳細(xì),需要的朋友一起看看吧2023-10-10