王新阳

wangxinyang

asp xmlhttp发送form表单、xml、json或文本等示例

'发送端
Dim http
set http=server.createobject("MSXML2.XMLHTTP")
'下面一行只有在发送表单时才需要添加
'http.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"
http.open "POST","http://127.0.0.1/respond.asp",false
http.send(表单、文本、json、xml)
If http.readystate=4 then
	'发送成功
	'如果接收数据为乱码,需要用adodb.stream进行转码
	'接收文本和json数据:text=http.ResponseText
	'接收xml:xml.Load(http.ResponseBody),只有响应端的内容类型是text/xml时,可以使用xml.Load(http.ResponseXML),也可以xml.LoadXML(http.ResponseText)
end if
Set http=Nothing

'响应端
'接收表单直接用Request.Form
'接收文本和json都是用adodb.stream对Request.BinaryRead(Request.TotalBytes)获取的二进制进行文本转换
'接收xml:xml.Load(Request.BinaryRead(Request.TotalBytes))
Dim xml
Set xml = Server.CreateObject("MSXML2.DOMDocument")
xml.preserveWhiteSpace = true
xml.async = false
'xml.Load(二进制)
xml.LoadXml(文本型xml)
If xml.parseError.errorCode <> 0 Then
	Response.Write "Description: " & xml.parseError.reason & "
Line: " & xml.parseError.Line
Else
	If xml.getElementsByTagName("name").length=0 Then
		Response.Write "节点不存在"
	Else
		Response.Write xml.getElementsByTagName("name").Item(0).text
	End If
End If
2015-07-22
2024-05-07 星期二 农历三月二十九