用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)文章
詳解ajax +jtemplate實現(xiàn)動態(tài)分頁
jtemplate是一個基于JQuery的模板引擎插件,功能非常強大,有了她你就再不用為使用JS綁定數(shù)據(jù)集而發(fā)愁了。本文給大家分享ajax +jtemplate實現(xiàn)動態(tài)分頁,需要的朋友可以參考下本文2015-09-09
ajax動態(tài)查詢數(shù)據(jù)庫數(shù)據(jù)并顯示在前臺的方法
今天小編就為大家分享一篇ajax動態(tài)查詢數(shù)據(jù)庫數(shù)據(jù)并顯示在前臺的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
ajax獲得json對象數(shù)組 循環(huán)輸出數(shù)據(jù)的方法
今天小編就為大家分享一篇ajax獲得json對象數(shù)組 循環(huán)輸出數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08

