The HttpRequest Object

本文介绍了HttpRequest对象,它可实现客户端与服务器的通信。文中说明了在IE 5.0中用JavaScript和VBScript创建该对象的代码,还阐述了如何用其从服务器获取XML文件、向服务器ASP页面发送XML文档,同时指出Microsoft XMLHTTP对象存在只能在浏览器运行的问题。

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

http://www.w3schools.com/dom/dom_http.asp

The HttpRequest object provides client-side communication with a server.

The HttpRequest object

With the httpRequest object you can send a request from the client to the server.

If you are using JavaScript in IE 5.0, you can create the httpRequest object with the following code:

var xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP")

If you are using VBScript you create the httpRequest object with the following code:

set xmlHTTP = CreateObject("Microsoft.XMLHTTP")

The httpRequest object is not a part of the W3C DOM standard.


Get XML

How to get an xml file from the server using the httpRequest object (works only in IE):

var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") xmlHttp.open("GET", "note.xml", false) xmlHttp.send() xmlDoc=xmlHttp.responseText

Try it Yourself

Netscape compatible code:

xmlHttp = new XMLHttpRequest(); xmlHttp.open("GET", "note.xml", false); xmlHttp.send(null); xmlDoc = xmlHttp.responseText;

Try it Yourself


Send XML

You can also send an xml document to an ASP page on the server, analyze the request, and send back the result.

var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") xmlHttp.open("POST", "demo_dom_http.asp", false) xmlHttp.send(xmlDoc) document.write(xmlHttp.responseText)

The ASP page, written in VBScript:

set xmldoc = Server.CreateObject("Microsoft.XMLDOM") xmldoc.async=false xmldoc.load(request) for each x in xmldoc.documentElement.childNodes if x.NodeName = "to" then name=x.text next response.write(name)

You send the result back to the client using the response.write property.

Try it Yourself


Important Note

At the moment, the Microsoft XMLHTTP object can only be run in the BROWSER.

SERVER code that attempts to use the XMLHTTP to communicate with other Web servers, may function incorrectly or perform poorly.

This is a bug in the HTTPRequest object. For more information read Microsoft's Knowledge Base article Q237906.

The rumor is that Microsoft will have this bug fixed in an upcoming release of the XML Library. In the meantime, you may have to use a commercially available ASPHTTP component.


The httpRequest Properties

PropertyDescription
readyStateReturns the state of the document
responseBodyReturns the response as an array of unsigned bytes
responseStreamReturns the response as an IStream
responseTextReturns the response as a string
responseXMLReturns the response as an xml document
statusReturns the status code as a number
statusTextReturns the status as a string

The httpRequest Methods

PropertyDescription
abort()Cancel the current http request
getAllResponseHeaders()Returns the value of the http headers
getResponseHeader(headerName)Returns the value of one specified http header
open(method, url, async, userid, password)Opens http request, and specifies the information
send()Send the http request to the server
setRequestHeader(headerName,headerValue)Specifies the name

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值