An introduction to the main objects, methods and attributes of Ajax experience 2--ajax

Source: Internet
Author: User

1.XMLHttpRequest objects
The XMLHttpRequest object is implemented on most browsers and has a simple interface that allows data to be delivered from the client to the server, but does not interrupt the user's current operation. Data transmitted using XMLHttpRequest can be in any format, although it is suggested from the first name as XML-formatted data.

XMLHttpRequest was first implemented in IE5 as an ActiveX component. Non-consortium standards. Create XMLHttpRequest objects (not uniform because of non-standard implementations). Internet Explorer implements XMLHttpRequest as an ActiveX object in other browsers (Firefox, Safari, Opera ...). ) to implement it as a local JavaScript object.

XMLHttpRequest implementations on different browsers are compatible, so you can access the properties and methods of the XMLHttpRequest instance in the same way, regardless of how the instance is created.

XMLHttpRequest Object Method

Method

Describe

Abort ()

Stop current request

getAllResponseHeaders ()

Returns all the response headers of an HTTP request as a key/value pair

getResponseHeader ("Headerlabel")

Returns the string value of the specified header

Open (Method,url,async)

Specify the type of request, the URL, and whether the request is processed asynchronously.

Method: The requested type; get or POST

URL: The location of the file on the server

Async:true (asynchronous) or false (synchronous)

Send (content)

Send a request to the server

setRequestHeader ("label", "value")

Sets the specified header to the supplied value. The open () must be called before any header is set.

XMLHttpRequest Object Properties

2. Send request--Introduction to methods and properties

Using the XMLHttpRequest instance to communicate with the server consists of the following 3 key sections: onReadyStateChange event handler, open method, send method.

onreadystatechange:

The event handler is triggered by the server, not by the user, and during Ajax execution the server notifies the client of the current state of communication. This is accomplished by updating the readyState of the XMLHttpRequest object. Changing the ReadyState property is a way for the server to connect to the client. Each readyState property change triggers the ReadyStateChange event.

open (method, URL, asynch):

The XMLHttpRequest object's Open method allows programmers to send requests to the server with an Ajax call. Method: The request type, a string similar to "get" or "POST". If you want to retrieve only one file from the server without sending any data, use get (you can send the data in a GET request by a query string attached to the URL, but the data size is limited to 2000 characters). If you need to send data to the server, use post. In some cases, some browsers cache the results of multiple XMLHttpRequest requests in the same URL. This can lead to bad results if the response to each request is different. Appending the current timestamp to the end of the URL ensures the uniqueness of the URL, thus avoiding the browser caching results. Like what:

var url = "getexample?timestamp=" + New Date (). GetTime ();

URL: The path string that points to the file on the server you are requesting. can be an absolute or relative path. Asynch: Indicates whether the request is to be transmitted asynchronously, and the default value is True (asynchronous). Specify True, you do not need to wait for the server to be appropriate before reading the following script. Specify false, and when the script process passes this, it stops and waits until the AJAX request is finished before continuing.

Send (data):

The Open method defines some details of the Ajax request. The Send method sends instruction data to a standby request: A string that will be passed to the server. If a GET request is selected, no data is sent and null can be passed to the Send method: Request.send (NULL), and when the Send () method is supplied with a parameter, ensure that the method specified in open () is post if no data is sent as part of the request body , NULL is used.

setRequestHeader (Header,value)

When the browser requests a page from the server, it sends a set of header information along with the request. These header information is a series of meta data (metadata) that describes the request. The header information is used to declare whether a request is a get or a POST.

In an AJAX request, the task of sending header information can be completed by setRequestHeader. Parameter header: The name of the header; Argument value: The value of the header. If you send data to the server with a POST request, you need to set the header of "Content-type" to "application/x-www-form-urlencoded". It tells the server that the data is being sent and that the data has been encoded with the URL. the method must be open () before it can be invoked.

3. Receive--Introduction to methods and properties

You can send a request to the server using the XMLHttpRequest method. The following properties of XMLHttpRequest can be changed by the server during the Ajax process: readyState

Status,responsetext, Responsexml.

Status

Each response sent by the server also has the header information. The three-digit status code is the most important header information in the response sent by the server and is part of the Hypertext Transfer Protocol.

Common status codes and their meanings:

404 Not Found page (NotFound)

403 No access (forbidden)

500 Internal Server error (Internalservice error)

2001-Cut Normal (OK)

304 Not modified (notmodified) (server returns 304 status, indicating source file not modified)

In the XMLHttpRequest object, the status code sent by the server is stored in the Status property. By comparing this value to 200 or 304, you can ensure that the server has sent a successful response

ResponseText

The XMLHttpRequest ResponseText property contains data sent from the server. It is a html,xml or plain text, depending on what the server sends.

When the ReadyState property value becomes 4 o'clock, the ResponseText property is available, indicating that the AJAX request has ended.

Responsexml

If the server returns XML, the data is stored in the Responsexml property. The Responsexml property is available only if the server sends data with the correct header information. MIME type must be Text/xml

Ajax Development Framework

Ajax essentially follows the request/server pattern, so the basic process of this framework is: object initialization, sending requests, server reception, server return, client receiving, modifying client page content. It's just that the process is asynchronous.

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.