用Ajax讀取XML格式的數據
更新時間: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>結果
var info = root.getElementsByTagName('info');
// 顯示返回結果
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>結果
var info = root.getElementsByTagName('info');
// 顯示返回結果
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ā)送數據xml格式接受數據
- 用Ajax讀取xml文件的簡單例子
- jQuery+ajax讀取并解析XML文件的方法
- Jquery Ajax學習實例 向頁面發(fā)出請求,返回XML格式數據
- javascript解析ajax返回的xml和json格式數據實例詳解
- jquery $.ajax()取xml數據的小問題解決方法
- JS通過ajax動態(tài)讀取xml文件內容的方法
- 通過AJAX的JS、JQuery兩種方式解析XML示例介紹
- JS使用ajax從xml文件動態(tài)獲取數據顯示的方法
- 用JQuery 實現AJAX加載XML并解析的腳本
- Ajax對xml信息的接收和處理操作實例分析
相關文章
JQuery ajax中error返回錯誤及一直返回error的解答
本文由腳本之家小編給大家分享有關 JQuery ajax中error返回錯誤及一直返回error的解答總結,需要的朋友可以參考下2015-09-09

