XMLHttpRequest 对象获取服务端数据

本文介绍如何利用XMLHttpRequest对象的ResponseXML属性从服务器端获取并解析XML数据。通过设置contentType为text/xml,确保XMLHttpRequest能够正确解析XML文件流。文章提供了一个具体的示例代码,演示了如何创建AJAX请求、发送请求以及处理返回的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们可以使用 XMLHttpRequest对象的ResponseXML属性获取服务器端数据。需要注意的是,ASP.NET 文件 index.aspx 的contentType 必须是 text/xml ,XMLHttpRequest.ResponseXML属性才能解析成XML文件流。

html 文件
 1None.gif<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
 2None.gif<html>
 3None.gif<head>
 4None.gif<title></title>
 5None.gif<script language="javascript" src="JScript1.js"></script>
 6None.gif</head>
 7None.gif<body>
 8None.gif    <input type="text" id="InputID">
 9None.gif    <input type="button" value="OK" onclick="ChangeValue();"> <br>
10None.gif    <div id="showHot"></div>
11None.gif</body>
12None.gif</html>
13None.gif

JS 文件
 1ExpandedBlockStart.gifContractedBlock.giffunction ajaxRequest()dot.gif{
 2InBlock.gif    Ajax = false;
 3ExpandedSubBlockStart.gifContractedSubBlock.gif    trydot.gif{
 4InBlock.gif       Ajax = new XMLHttpRequest();
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    }
catch(ee)dot.gif{
 6ExpandedSubBlockStart.gifContractedSubBlock.gif       trydot.gif{
 7InBlock.gif               Ajax = new ActiveXObject("Msxml.XMLHTTP");        
 8ExpandedSubBlockStart.gifContractedSubBlock.gif       }
catch(e)dot.gif{
 9ExpandedSubBlockStart.gifContractedSubBlock.gif               trydot.gif{
10InBlock.gif               Ajax = new ActiveXObject("Msxml.XMLHTTP");        
11ExpandedSubBlockStart.gifContractedSubBlock.gif               }
catch(e)dot.gif{
12ExpandedSubBlockStart.gifContractedSubBlock.gif               trydot.gif{
13InBlock.gif                       Ajax = new ActiveXObject("Microsoft.XMLHTTP");                
14ExpandedSubBlockStart.gifContractedSubBlock.gif               }
catch(E)dot.gif{
15InBlock.gif                   Ajax = false;
16ExpandedSubBlockEnd.gif               }

17ExpandedSubBlockEnd.gif               }

18ExpandedSubBlockEnd.gif       }

19ExpandedSubBlockEnd.gif     }

20ExpandedBlockEnd.gif}

21None.gif
22ExpandedBlockStart.gifContractedBlock.giffunction getAjaxValue(strUserID)dot.gif{    
23InBlock.gif    ajaxRequest();
24ExpandedSubBlockStart.gifContractedSubBlock.gif    if(!Ajax)dot.gif{
25InBlock.gif        alert("Ajax its not working");
26InBlock.gif        return;
27ExpandedSubBlockEnd.gif    }

28InBlock.gif    var xmlValue = new String();
29InBlock.gif    Ajax.open("GET""index.aspx?userid="+strUserID,false)
30InBlock.gif    Ajax.send(null);
31InBlock.gif    var xmldoc = Ajax.responseXML.getElementsByTagName("div");
32InBlock.gif    if(xmldoc.length > 0)
33ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
34InBlock.gif        for(var i=0;i<xmldoc.length;i++)
35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
36InBlock.gif            xmlValue += (i+1+""+ xmldoc[i].firstChild.data + "<br>";
37ExpandedSubBlockEnd.gif        }

38ExpandedSubBlockEnd.gif    }

39InBlock.gif    
40InBlock.gif    return xmlValue;
41ExpandedBlockEnd.gif}

42None.gif
43None.giffunction ChangeValue()
44ExpandedBlockStart.gifContractedBlock.gifdot.gif{
45InBlock.gif    var strvalue = "";
46InBlock.gif    strvalue = document.getElementById("InputID").value;
47InBlock.gif    var setValue = document.getElementById("showHot");
48InBlock.gif    setValue.innerHTML = getAjaxValue(strvalue);
49ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/Yuqui1980/archive/2007/04/29/732223.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值