Basic knowledge of HTTP messages and ajax,

Source: Internet
Author: User
Tags ajax status code

Basic knowledge of HTTP messages and ajax,

HTTP packets

The content that the client sends to the server and the content that the server sends to the client belong to the HTTP packet.

Start row: the start row of the request's start row.

Header: Request Header Response Header General header (both request and response) custom Header

Body: Request body response body

The client transmits data to the server:

The request URL is followed by a question mark to pass the parameter to the server/getList? Name = zhangsan & age = 7

Set the Request Header (Set Request Header Information)

Set the Request body and place the content passed to the server in the Request body and send it to the server.

The server sends the data to the client:

Set response header information

Set response body content

Ajax: async javascript and xml asynchronous JS and XML (eXtensible Markup Language, the labels used in XML files are all self-extended, store related data using its own extended, rule-based tags)

Ajax is a method in client JS used to send requests to the server (or to transmit data to the server ), then obtain the content returned by the server (Ajax is generally run in the browser of the client ).

Ajax Step 4:

Create an Ajax object (this method is not supported in IE6 and earlier browsers)

var xhr = new XMLHttpRequest;

Configure basic information before sending: configure the Request Method (GET, POST, PUT, DELETE, HEAD ...)

Open a URL (Configure which server address to send a request)

Synchronous or asynchronous (true indicates asynchronous false indicates that the default value of synchronization is true)

[Username] user name for the request to the server

[Userpass] the user password for the request to the server. These two values are generally not written. Only when the server imposes security restrictions and only allows access by specific users can we pass them through.

xhr.open('get',"/data.txt",false,[username],[userpass]);

Bind a method to the onreadystatechange event to listen for changes in the status (trigger method execution as long as the status changes)

xhr.onreadystatechange = function(){

Xhr. readyState: AJAX status code, which can represent the progress of the current processing

0 unset the current request has not been sent

1. The opened URL address is enabled (the parameter configuration before sending is complete)

2. The headers_received response header is accepted.

3. The content returned by loading is being prepared on the server.

4. The done response body has been successfully returned to the client.

Xhr. status: HTTP Network status Code, which describes the status of the server response content

200 OR ^ 2 \ d {2} (200 OR starts with 2)-> indicates that the content of the response body has been successfully returned.

301 permanent redirect/permanent transfer

302 temporary redirection/Server Load balancer for temporary transfer

304 The retrieved content is to read the data in the cache.

400 An error occurred while passing parameters from the client to the server.

401 no access permission

404 The client access address does not exist

500 Unknown Server Error

503 the server is overloaded

  if(xhr.readyState===4 && /^2\d{2}$/.test(xhr.status)){    var val = xhr.responseText;  }}

Send request: the parameter is the content transmitted to the server by the request body.

xhr.send(null);

Summary

The above section describes the basic knowledge of HTTP messages and ajax. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.