Ajax basics of XMLHttpRequest Object summary _ajax related

Source: Internet
Author: User
Tags http request
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 established, but not initialized (the open method has not been called)
1 (initialization) object established, send method has not been called
2 (send data) The Send method has been invoked, but the current state and HTTP headers are unknown
3 (data transfer) has received part of the data, because the response and HTTP headers are incomplete, then get some data through Responsebody and ResponseText error,
4 (complete) data received, at this time through Responsebody and responsetext to obtain complete response data
Four: Responsebody
Returns the server response data in a format
V: Responsestream
Returns a response letter in the form of an ADO Stream object
VI: ResponseText
Returns the response information as a string
Note
Variable, this property is read-only, and the response information is returned as a string.
XMLHTTP attempts to decode the response information to a Unicode string, xmlhttp the response data by default to UTF-8, and if the server returns data with a 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 process the encoding declaration in the XML document. You need to use Responsexml to handle it.
Seven: Responsexml format the response information as an XML Document object and return
Note
Variable, this property is read-only, and the response information is formatted as an XML Document object and returned. If the response data is not a valid XML document, the 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 for the current request
Long shaping standard HTTP status code, defined as follows:
Number Description
100
Continue
101
Switching protocols
200
Ok
201
Created
202
Accepted
203
Non-authoritative Information
204
No Content
205
Reset Content
206
Partial Content
300
Multiple choices
301
Moved Permanently
302
Found
60V
Other
304
Not Modified
305
Use Proxy
307
Temporary Redirect
400
Bad Request
60s
Unauthorized
402
Payment Required
403
Forbidden
404
Not Found
405
Method Not allowed
50W
Not acceptable
407
Proxy Authentication Required
408
Request Timeout
409
Conflict
410
Gone
411
Length Required
412
Precondition Failed
413
Request Entity Too Large
414
Request-uri Too Long
415
Unsupported Media Type
416
Requested Range not suitable
417
Expectation Failed
500
Internal Server Error
501
Not implemented
502
Bad Gateway
503
Service unavailable
504
Gateway Timeout
505
HTTP Version not supported
Note
Long shaping, this property is read-only and returns the HTTP status code for the current request, which is only available when the data is sent and received.
Nine: Xmlhttprequest:statustext members
StatusText
Returns the response line status of the current request
Ten: Abort () method
Cancel the current request
11: getAllResponseHeaders () method
Gets all HTTP headers for the response
Note
Each HTTP header name and value is separated by a colon and ends with \ r \ n. The method cannot be called until the Send method completes.
12: getResponseHeader Gets the specified HTTP header from the response information
Cases:
Xmlhttp.getresponseheader ("Server");
The server column in the output HTTP header: The version and name of the current Web server.
Note
The method can be called when the Send method succeeds. If the server returns a document type of "Text/xml", the sentence is Xmlhttp.getresponseheader ("Content-type"), and the string "Text/xml" is returned. You can use the getAllResponseHeaders method to get complete HTTP header information.
13: The Open () method creates a new HTTP request and specifies the method, URL, and authentication information for this request
Grammar
Xmlhttprequest.open (Strmethod, strURL, Blnasync, struser, strpassword);
Parameters
Strmethod
HTTP methods, for example: POST, get, put, and propfind. Not sensitive to case.
strURL
The URL address of the request, which can be either an absolute address or a relative address.
blnasync[Optional]
Boolean, specifying whether this request is asynchronous and true by default. If true, the callback function specified by the onReadyStateChange property is invoked 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; if it is synchronous (false), the client waits until the server returns the message before performing another operation. We need to specify the synchronization according to the actual needs, in some pages, may issue multiple requests, even organized and planned to have a large scale of the high-intensity request, the latter is to cover the previous one, this time of course to specify the synchronization mode: Flase.
struser[Optional]
If the server requires authentication, the user name is specified here, and if not specified, the validation window pops up when the server needs to authenticate.
strpassword[Optional]
The password portion of the authentication information, which is ignored if the user name is empty.
Note
After calling this method, you can call the Send method to send data to the server.
14: Send () method
Send request to HTTP server and receive response
Xmlhttprequest.send (Varbody);
Parameters
Varbody
Data to be sent through this request.
Note
The way this method is synchronized or asynchronous depends on the Basync parameter in the open method, and if Basync = = False, this method will wait for the request to complete or time out to return, and if Basync = = True, this method returns immediately.
XV: setRequestHeader a single HTTP header that specifies the request
Example:
Copy Code code as follows:

var http_request= false;
function send_request (URL)
{///initialization, specifying handler functions, sending requests

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;
}
Specifies how the client handles when the server returns information
Http_request.onreadystatechange = ProcessRequest;
<span twffan= "Done" > Determine the way and URL to send the request and whether to execute the next piece of code synchronously
Http_request.open ("Get", url,true);
Http_request.send (NULL);
}
//******************************************************************
function ProcessRequest ()
{
if (http_request.readystate = = 4)//Judging object status
{
if (Http_request.status = 200)//The result of the request has been successfully returned
{
alert (http_request.responsebody);
var a = document.getElementById ("hh"). innertext;
if (A = = "1")
{
Alert ("Not available!! ");
}
}
else//page is not normal
{
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)
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.