您的位置:首页 > 理论基础 > 计算机网络

xmlhttp:演示后台POST一个XML数据流到数据库的示例

2005-10-27 23:51 501 查看
test.htm
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
</HEAD>
<BODY>
<form method="post" action="postxml.asp">
<P><INPUT name="mobile" type="text"></P>
<P><INPUT id=button1 type=submit ></P>

</form>
</BODY>
</HTML>

postxml.asp文件
<%@ Language=VBScript %>
<HTML>
<BODY>
<%
dim v
v=Request("mobile")
dim i, strxml
'构造xml串
strxml = "<?xml version='1.0' encoding='gb2312' ?>"
strxml = "<chuandi>"
strxml = strxml & "<mobile>" & v & "</mobile>"
strxml = strxml & "</chuandi>"
%>

<%
'使用xmlhttp发送
dim xmlhttp
set xmlhttp = server.CreateObject("Microsoft.XMLHTTP")
xmlhttp.Open "POST", "http://localhost/xml/xmlhttp/getxml.asp", False
xmlhttp.send strxml

'如果错误
If xmlhttp.Status <> 200 Then
Response.Write "发生了错误"
Response.End
End If
这里的功能主要是得到提交后返回的信息
ResponseXml返回有一些问题,ResponseText返回正常

'直接使用ResponseText可以得到
response.Write xmlhttp.ResponseText

%>

</BODY>
</HTML>

getxml.asp文件,你可以不需要数据库,我只是为了测试,需要自己建一个access表

<%@ Language=VBScript %>
<%
set conn=Server.CreateObject("ADODB.connection")
conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & Server.MapPath("web.mdb")

'这里需要修改,返回客户的xml响应文档
Response.ContentType = "text/xml"

dim xmldom
' set xmldom = Server.CreateObject("Microsoft.XMLDOM")
set xmldom = Server.CreateObject("MSXML.DOMDocument")
xmldom.load Request

dim mobile
mobile = xmldom.selectSingleNode("//mobile").text
on error resume next
'使用数据库只是为了测试数据传递过来
strsql="insert into test(sid,name) values('001','"& mobile &"')"
'response.write strsql
conn.execute strsql
if err.number=0 then
retval="数据成功提交"
else
retval="数据提交失败,请检查你的数据"
end if

set xmldom = Nothing
set conn=nothing
'下面的代码是将结果以xml形式返回
'需要加上<?xml version="1.0" encoding="gb2312"?>这句,否则无法返回中文
%>
<?xml version="1.0" encoding="gb2312"?>
<kaitong>
<chengong>Y</chenggong>
<mobile><%=mobile%></mobile>
</kaitong>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐