Discussion on AJAX client request and service-side response

Source: Internet
Author: User

AJAX, the asynchronous Javascript and Xml,ajax, essentially communicates with the server asynchronously, based on the HTTP protocol.

The so-called asynchronous, refers to the execution of a program does not block the execution of other programs, its representation is that the order of execution of the program does not depend on the writing order of the program itself.
thereby improving overall execution efficiency.

1: Client Request
JS built-in HTTP request object XMLHttpRequest;
A, request line
Xhr.open () initiates a request, which can be a get, post method

Differences in how get and post requests are

1, get no request body, use Xhr.send (null)

2. Get can add request parameters on the request URL

3, post can be through Xhr.send (' name=itcast&age=10 ')

4. Post needs to be set

5, get more efficient (more applications)

6, get size limit about 4k,post There is no limit
B, Request header
Xhr.setrequestheader () Setting the request header
C. Request Content
Xhr.send () Send request principal get mode using xhr.send (NULL)

2: Service-side corresponding
The HTTP response is sent by the server, because it takes time (such as a slow speed) to respond, so we need to listen to the status of the server's response before it can be processed.
A, get status lines (including status codes & status information)
Xhr.status status code, such as 200, 304, 404, etc. xhr.statustext status code information such as OK;
b, get the response header
Xhr.getresponseheader (' Content-type ') gets the specified header information; xhr.getallresponseheaders (); Gets all the response header information;
C, Response body
Xhr.responsetext Xhr.responsexml all represent the response body, we need to detect and determine the MIME type of the response header to use Request.responsetext or request.responsexml.

onReadyStateChange is a type of JavaScript event that is meant to monitor the state of XMLHttpRequest, in various states as follows:
**readystate**
0: The request was not initialized (open () has not been called yet).
1: The request has been established but has not yet been sent (the Send () has not been called).
2: The request has been sent, is in process (usually can now get the content header from the response).
3: The request is in process, and some of the data in the response is usually available, but the server has not finished generating the response.
4: The response is complete; you can get and use the server's response.

Discussion on AJAX client request and service-side response

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.