用Ajax讀取XML格式的數(shù)據(jù)
更新時間:2006年11月06日 00:00:00 作者:
復制代碼 代碼如下:
<!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對象
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對象
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
復制代碼 代碼如下:
<?xml version="1.0" encoding="GB2312" ?>
<root>
<info>hello world!</info>
</root>
您可能感興趣的文章:
- AJAX使用post發(fā)送數(shù)據(jù)xml格式接受數(shù)據(jù)
- 用Ajax讀取xml文件的簡單例子
- jQuery+ajax讀取并解析XML文件的方法
- Jquery Ajax學習實例 向頁面發(fā)出請求,返回XML格式數(shù)據(jù)
- javascript解析ajax返回的xml和json格式數(shù)據(jù)實例詳解
- jquery $.ajax()取xml數(shù)據(jù)的小問題解決方法
- JS通過ajax動態(tài)讀取xml文件內(nèi)容的方法
- 通過AJAX的JS、JQuery兩種方式解析XML示例介紹
- JS使用ajax從xml文件動態(tài)獲取數(shù)據(jù)顯示的方法
- 用JQuery 實現(xiàn)AJAX加載XML并解析的腳本
- Ajax對xml信息的接收和處理操作實例分析
相關(guān)文章
JQuery ajax中error返回錯誤及一直返回error的解答
本文由腳本之家小編給大家分享有關(guān) JQuery ajax中error返回錯誤及一直返回error的解答總結(jié),需要的朋友可以參考下2015-09-09使用ajax技術(shù)無刷新動態(tài)調(diào)用股票信息
新浪的財金頻道一直感覺做得很好。但由于最近網(wǎng)速慢的緣故,查看股票信息時網(wǎng)頁老是打不開。這幾天一直在研究ajax,于是用jquery自己做了一個自動讀取新浪股票實時數(shù)據(jù)的頁面。2008-11-11