Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of XMLHttpRequest object in ajax

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what the role of XMLHttpRequest objects in ajax, I believe most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

XMLHttpRequest provides a protocol for clients to communicate with http servers.

One: create

IE: http_request = new ActiveXObject ("Msxml2.XMLHTTP")

Http_request = new ActiveXObject ("Microsoft.XMLHTTP")

Non IE: http_request = new XMLHttpRequest ()

Two: onreadystatechange

Specifies the event handling handle when the readyState property changes

Grammar

Http_request .onreadystatechange = funcMyHandler

Three: readyState

Returns the current status of the XMLHTTP request

Variable, this property is read-only, and the state is represented by an integer of length 4. The definition is as follows:

0 (uninitialized) object has been established, but has not been initialized (open method has not been called)

1 (initialization) object has been established and the send method has not been called

2 (send data) the send method has been called, but the current state and http header are unknown

3 (data transfer) has received part of the data, because the response and http headers are incomplete, there will be an error in obtaining part of the data through responseBody and responseText

4 (complete) the data has been received, and the complete response data can be obtained through responseBody and responseText

Four: responseBody

Returns server response data in a certain format

Five: responseStream

Returns a response letter in the form of an Ado Stream object

Six: responseText

Return the response information as a string

Remarks

Variable, this property is read-only and returns the response information as a string.

XMLHTTP attempts to decode the response information into a Unicode string. By default, XMLHTTP encodes the response data as UTF-8. If the data returned by the server has BOM (byte-order mark), XMLHTTP can decode any UCS-2 (big or little endian) or UCS-4 data. Note that if the server returns an xml document, this property does not handle the encoding declaration in the xml document. You need to use responseXML to handle it.

Seven: responseXML formats the response information as a Xml Document object and returns

Remarks

Variable, which is read-only, formats the response information as a Xml Document object and returns. If the response data is not a valid XML document, this property itself does not return XMLDOMParseError, and the error message can be obtained through the processed DOMDocument object.

Eight: status returns the http status code of the current request

The standard http status code for long shaping is defined as follows:

Number Description

one hundred

Continue

one hundred and one

Switching protocols

two hundred

OK

two hundred and one

Created

two hundred and two

Accepted

two hundred and three

Non-Authoritative Information

two hundred and four

No Content

two hundred and five

Reset Content

two hundred and six

Partial Content

three hundred

Multiple Choices

three hundred and one

Moved Permanently

three hundred and two

Found

three hundred and three

See Other

three hundred and four

Not Modified

three hundred and five

Use Proxy

three hundred and seven

Temporary Redirect

four hundred

Bad Request

four hundred and one

Unauthorized

four hundred and two

Payment Required

four hundred and three

Forbidden

four hundred and four

Not Found

four hundred and five

Method Not Allowed

four hundred and six

Not Acceptable

four hundred and seven

Proxy Authentication Required

four hundred and eight

Request Timeout

four hundred and nine

Conflict

four hundred and ten

Gone

four hundred and eleven

Length Required

four hundred and twelve

Precondition Failed

four hundred and thirteen

Request Entity Too Large

four hundred and fourteen

Request-URI Too Long

four hundred and fifteen

Unsupported Media Type

four hundred and sixteen

Requested Range Not Suitable

four hundred and seventeen

Expectation Failed

five hundred

Internal Server Error

five hundred and one

Not Implemented

five hundred and two

Bad Gateway

five hundred and three

Service Unavailable

five hundred and four

Gateway Timeout

five hundred and five

HTTP Version Not Supported

Remarks

Long shaping, this property is read-only and returns the http status code of the current request. This property can be obtained only after the data has been sent and received.

Nine: xmlhttprequest:statusText members

StatusText

Returns the response line status of the current request

Ten: abort () method

Cancel the current request

Eleven: getallresponseheaders () method

Get all http headers of the response

Remarks

Each http header name and value is separated by a colon and ends with\ r\ n. The send method cannot be called until it is complete.

Twelve: getResponseHeader gets the specified http header from the response information

Example:

Xmlhttp.getResponseHeader ("Server")

Output the server column in the http header: the version and name of the current web server.

Remarks

The send method can only be called when it is successful. This sentence xmlhttp.getResponseHeader ("Content-Type"); returns the string "text/xml" if the document type returned by the server is "text/xml". You can use the getAllResponseHeaders method to get the complete http header information.

Thirteen: the open () method creates a new http request and specifies the method, URL, and authentication information for the request

Grammar

XMLHttpRequest.open (strMethod, strUrl, blnAsync, strUser, strPassword)

Parameters.

StrMethod

Http methods, such as POST, GET, PUT, and PROPFIND. Case insensitive.

StrUrl

The requested URL address can be an absolute address or a relative address.

BlnAsync [optional]

Boolean that specifies whether the request is asynchronous. The default is true. If true, the callback function specified by the onreadystatechange property is called when the state changes.

Async is a Boolean value. In the case of asynchronous communication (true), the client does not wait for a response from the server; in the case of synchronous communication (false), the client waits for the server to return a message before performing anything else. We need to specify the synchronization method according to the actual needs. In some pages, multiple requests may be issued, or even organized, planned and formed large-scale high-intensity request, and the latter will overwrite the previous one. At this time, of course, specify the synchronization method: Flase.

StrUser [optional]

If the server needs authentication, specify the user name here, and if not, the authentication window will pop up when the server needs authentication.

StrPassword [optional]

The password section of the authentication information, which is ignored if the user name is empty.

Remarks

After calling this method, you can call the send method to send data to the server.

Fourteen: send () method

Send a request to the http server and receive a response

XMLHttpRequest.send (varBody)

Parameters.

VarBody

The data to be sent through this request.

Remarks

The synchronous or asynchronous mode of this method depends on the bAsync parameter in the open method. If bAsync = = False, this method will not return until the request completes or times out. If bAsync = = True, this method will return immediately.

Fifteen: setRequestHeader specifies a http header of the request separately.

Example:

The copy code is as follows:

Var http_request= false

Function send_request (url)

{/ / initialize, specify the handling function, send the request function

If (window.XMLHttpRequest) / / Mozila

{

Http_request = new XMLHttpRequest ()

If (http_request.overrideMimeType)

{

Http_request.overrideMimeType ("text/xml")

}

}

Else

If (window.ActiveXobject) / / IE

{

Try

{

Http_request = new ActiveXObject ("Msxml2.XMLHTTP")

}

Catch (e)

{

Try {

Http_request = new ActiveXObject ("Microsoft.XMLHTTP")

}

Catch (e) {}

}

}

If (! http_request) / / exception, failed to create object instance

{

Alert ("cannot create XMLHttpRequest instance!")

Return false

}

/ / specify how to handle the client when the server returns information

Http_request.onreadystatechange = processRequest

/ / determine how to send the request and URL, and whether to execute the next code synchronously

Http_request.open ("GET", url,true)

Http_request.send (null)

}

/ / *

Function processRequest ()

{

If (http_request.readyState = = 4) / / determine the status of the object

{

If (http_request.status = = 200) / / the request result has been returned successfully

{

Alert (http_request.responseBody)

Var a = document.getElementById ("hh") .innerText

If (a = "1")

{

Alert ("not available!")

}

}

The else / / page is abnormal.

{

Alert ("the page you requested is not normal")

}

}

}

/ / *

Function userCheck ()

{

Var f = document.Form1

Var userName = f.username.value

If (userName = "")

{

Alert ("user name cannot be empty!")

F.username.focus ()

Return false

}

Else

{

Send_request ("alert.aspx?username=" + userName)

}

}

The above is all the content of the article "what is the role of XMLHttpRequest objects in ajax". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report