In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is XMLHttpRequest object". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is XMLHttpRequest object".
The first thing, of course, is to take a look at the XMLHttp object in the browser:
XMLHTTP method:
Note: the client can use the XMLHTTP object to send any HTTP request, accept the HTTP reply, and parse the answered XML document.
Open method: initializes a Msxml2.XMLHTTP request, specifying the HTTP request method, URL, and authentication information.
Syntax:
Open (bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword)
Parameter description:
BstrMethod: data transfer method, that is, GET or POST.
BstrUrl: the URL of the service web page.
VarAsync: whether to execute synchronously. The default is True, which means synchronous execution, but synchronous execution can only be implemented in DOM. In use, it is generally set to False, that is, asynchronous execution.
BstrUser: user name, can be omitted.
BstrPassword: user password, which can be omitted.
Send method: sends a HTTP request to the server and returns a reply.
Syntax:
OXMLHttpRequest.send (varBody)
Note: whether this method is synchronized depends on the varAsync parameter of the Open method. If set to True, the call returns immediately, and if set to False, the call does not return until the entire reply is received.
SetRequestHeader (bstrHeader, bstrvalue)
BstrHeader:HTTP header (header)
Bstrvalue: value of HTTP header (header)
If the Open method is defined as POST, you can define the form to upload:
Xmlhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded")
XMLHTTP attribute:
Onreadystatechange: gets the event handle that returns the result in synchronous execution mode. Can only be called in DOM.
ResponseBody: the result returns as an array of unsigned integers.
ResponseStream: the result is returned as an IStream stream.
ResponseText: the result is returned as a string.
ResponseXML: the result is returned as data in XML format.
Using this principle, you can also do a network thief program. Web crawlers should be done with this thing, but I haven't done it. I may make one to play in the near future. Here we mainly look at how it is encapsulated in CS:
1//Ajax Start
2According to racing, racing /
3Universe / create callback object. Return this object if there is a window.XMLHttpRequest () object. If it is IE, search for each version of Msxml2.XMLHTTP and Microsoft.XMLHTTP and create an object to return.
4///
5function Ajax_GetXMLHttpRequest () {
6 if (window.XMLHttpRequest) {
7 return new XMLHttpRequest ()
8} else {
9 if (window.Ajax_XMLHttpRequestProgID) {
Return new ActiveXObject (window.Ajax_XMLHttpRequestProgID)
} else {
Var progIDs = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]
For (var I = 0; I
< progIDs.length; ++i) { var progID = progIDs[i]; try { var x = new ActiveXObject(progID); window.Ajax_XMLHttpRequestProgID = progID; return x; } catch (e) { } } } } return null; } /**//// ///Ajax回调。 /// ///调用服务端函数所在的类包括命名空间(如:NExplus.Controls.SiteHeader)。 ///客户端所对应的标记的ID(如:)。 ///服务端(方法)函数名称(被AjaxMethod标记)。 ///传到服务器的字符串。 ///同步或异步回调。 ///调试/请求字符串。 ///调试/输出字符串。 ///调试的错误信息。 ///是否和控件及其值一起回调。 ///Url地址。 function Ajax_CallBack(type, id, method, args, clientCallBack, debugRequestText, debugResponseText, debugErrors, includeControlValuesWithCallBack, url) { if (!url) { url = _window.location.href; url = url.replace(/\#.*$/, '');//去除URL中标签部分,即"#"之后的字符串。 //加入参数Ajax_CallBack并设为true,说明是AJAX回调。 if (url.indexOf('?') >-1)
Url + = "& Ajax_CallBack=true"
Else
{
If (url.substr (url.length-1,1) = "/")
Url + = "default.aspx"
Url + = "? Ajax_CallBack=true"
}
}
Var x = Ajax_GetXMLHttpRequest (); / / get the XMLHttpRequest object.
Var result = null
If (! X) {
Result = {"value": null, "error": "NOXMLHTTP"}
If (debugErrors) {
Alert ("error:" + result.error)
}
If (clientCallBack) {
ClientCallBack (result)
}
Return result
}
X.open ("POST", url, clientCallBack? True: false); / / Open the object in Post so that the parameters can be obtained with Request.Form on the server side.
X.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
If (clientCallBack) {
/ / if synchronized, judge the status and output an error message.
X.onreadystatechange = function () {
Var result = null
If (x.readyState! = 4) {
Return
}
If (debugResponseText) {
Alert (x.responseText)
}
Try
{
Var result = eval ("(" + x.responseText + ")")
If (debugErrors & & result.error) {
Alert ("error:" + result.error)
}
}
Catch (err)
{
If (window.confirm ('The following error occured while processing an AJAX request:' + err.message +'\ n\ nWould you like to see the response?'))
{
Var w = window.open ()
W.document.open ('text/plain')
W. [XSS _ clean] (x.responseText)
W.document.close ()
}
Result = new Object ()
Result.error ='An AJAX error occured. The response is invalid.'
}
ClientCallBack (result)
}
}
Var encodedData = "Ajax_CallBackType=" + type
If (id) {
EncodedData + = & Ajax_CallBackID= + id.split ("$") .join (":")
}
EncodedData + = "& Ajax_CallBackMethod=" + method
If (args) {
For (var i in args) {
EncodedData + = "& Ajax_CallBackArgument" + I + "=" + encodeURIComponent (args [I])
}
}
/ / if a control is added, the control data is added.
If (includeControlValuesWithCallBack & & document.forms.length > 0) {
Var form = document.forms [0]
For (var I = 0; I < form.length; + + I) {
Var element = form.elements [I]
If (element.name) {
Var elementValue = null
If (element.nodeName = = "INPUT") {
Var inputType = element.getAttribute ("TYPE") .toUpperCase
If (inputType = = "TEXT" | | inputType = = "PASSWORD" | | inputType = = "HIDDEN") {
ElementValue = element.value
} else if (inputType = = "CHECKBOX" | | inputType = = "RADIO") {
If (element.checked) {
ElementValue = element.value
}
}
} else if (element.nodeName = = "SELECT") {
ElementValue = element.value
} else if (element.nodeName = = "TEXTAREA") {
ElementValue = element.value
}
If (elementValue) {
EncodedData + = "&" + element.name + "=" + encodeURIComponent (elementValue)
}
}
}
}
/ / if it is debugging, the data sent is popped up.
If (debugRequestText) {
Alert (encodedData)
}
X.send (encodedData); / / sends data to the server.
If (! clientCallBack) {
If (debugResponseText) {
Alert (x.responseText)
}
Result = eval ("(" + x.responseText + ")")
If (debugErrors & & result.error) {
Alert ("error:" + result.error)
}
}
Delete x
Return result
}
/ / Ajax End
Thank you for your reading, the above is the content of "what is the XMLHttpRequest object", after the study of this article, I believe you have a deeper understanding of what is the XMLHttpRequest object, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.