Ajax Quick Start-http protocol Basics

Source: Internet
Author: User
The key to understanding Ajax is to understand Hypertext Transfer Protocol (HTTP), which is used to transmit web pages, images, and other types of files transmitted between browsers and servers over the Internet. As long as you enter a URL in your browser, the first http: // indicates that HTTP is used to access the information at the specified location. (Most browsers also support different protocols. FTP is a typical example .)

Note: This article only covers the HTTP protocol, which is a concern of Ajax developers. It can be used as a reference manual or guide for HTTP.

HTTP consists of two parts: request and response. When you enter a URL in a Web browser, the browser creates and sends a request based on your requirements. The request contains the entered URL and information related to the browser. When the server receives this request, it will return a response that includes information related to the request and data located in the specified URL (if any. Until the browser parses the response and displays the webpage (or other resources.

HTTP Request

The HTTP request format is as follows:

<Request-line>
<Headers>
<Blank line>
[<Request-body>]

In an HTTP request, the first line must be a request line to describe the request type, resources to be accessed, and the HTTP Version Used. Next is a header section, which describes additional information to be used by the server. After the header is a blank line, you can add any other data [called the body].

HTTP defines a large number of request types, but Ajax developers only care about GET requests and post requests. As long as you enter a URL in the web browser, the browser will send a GET request to the server based on the URL to tell the server what resources are obtained and returned. The GET request for www.wrox.com is as follows:

Get, HTTP, 1.1
HOST: www.wrox.com
User-Agent: Mozilla/5.0 (windows; U; Windows NT 5.1; en-US; RV: 1.7.6)
Gecko/20050225 Firefox/1.0.1
Connection: keep-alive

The first part of the request line indicates that the request is a GET request. The second part of the row is a slash (/), which indicates that the request is the root directory of the domain name. The last part of this row indicates that HTTP 1.1 is used (another option is 1.0 ). So where can I send the request? This is the content of the second line.

Row 2nd is the first request header, host. The first host indicates the request destination. Combined with the forward slash (/) in the previous line, you can notify the server that the request is www.wrox.com/(HTTP 1.1 requires the first host, but the original version 1.0 does not need to be used ). The third line contains the first User-Agent, which can be accessed by both the server and client scripts. It is an important basis for browser type detection logic. This information is defined by your browser (Firefox 1.0.1 in this example) and will be automatically sent in each request. The last line is the first connection. Usually, the browser operation is set to keep-alive (of course other values can be set, but this is beyond the scope discussed in this book ). Note that there is a blank line after the last header. This empty line is required even if the request body does not exist.

If you want to obtain a page in the www.wrox.com/booksdomain such as http://www.wrox.com/books, the request can be based on:

Getbooks/HTTP/1.1
HOST: www.wrox.com
User-Agent: Mozilla/5.0 (windows; U; Windows NT 5.1; en-US; RV: 1.7.6)
Gecko/20050225 Firefox/1.0.1
Connection: keep-alive

Note that only the content of the first line has changed. It only contains the part after www.wrox.com in the URL.

To send the GET request parameters, these additional information must be appended to the URL itself. The format is similar:

URL? Name1 = value1 & name2 = value2 &... & Namen = valuen

This information is called a query string. It will be copied in the request line of the HTTP request, as shown below:

GET/books /? Name = Professional % 20 Ajax HTTP/1.1
HOST: www.wrox.com
User-Agent: Mozilla/5.0 (windows; U; Windows NT 5.1; en-US; RV: 1.7.6)
Gecko/20050225 Firefox/1.0.1
Connection: keep-alive

Note: To use the text "professional Ajax" as the URL parameter, encode the content and replace the space with % 20, which is called URL encoding ), it is often used in many aspects of HTTP (JavaScript provides built-in functions to process URL encoding and decoding, which will be described later in this chapter ). Name-value pairs are separated. The vast majority of server-side technologies can automatically decode the Request body and provide some logic for access to these values. Of course, the server determines how to use the data.

The browser sends more headers than is discussed in this article. For the sake of simplicity, the example here is as short as possible.

On the other hand, the POST request provides additional information for the server in the Request body. Generally, when you fill in an online form and submit it, the entered data will be sent to the server as a POST request.

The following is a typical POST request:

Post, HTTP, 1.1
HOST: www.wrox.com
User-Agent: Mozilla/5.0 (windows; U; Windows NT 5.1; en-US; RV: 1.7.6)
Gecko/20050225 Firefox/1.0.1
Content-Type: Application/X-WWW-form-urlencoded
Content-Length: 40
Connection: keep-alive
Name = Professional % 20 Ajax & publisher = Wiley

We can see from the above that there are some differences between post requests and GET requests. First, the get at the beginning of the request line is changed to post to indicate different request types. You will find that the header host and User-Agent still exist, and there are two new lines behind it. The Content-Type Header describes how the content of the request body is encoded. The browser always transmits data in the format of application/X-WWW-form-urlencoded, which is a MIME type for simple URL encoding. The Content-Length header specifies the number of bytes of the Request body. A blank line is followed by the request body after the first connection. Like most browser POST requests, this is given in the form of a simple "name-value" pair, where name is professional Ajax and publisher is Wiley. You can organize URL query string parameters in the same format.

As mentioned above, there are other HTTP request types that follow the same basic format as get and post requests. Next, let's see what response the server will send to the HTTP request.

HTTP Response

As shown below, the HTTP Response format is very similar to the request format:

<Status-line>
<Headers>
<Blank line>
[<Response-body>]

As you can see, the only real difference in response is that the request information is replaced by the status information in the first line. Status line describes the requested resources by providing a status code. The following is an example of an HTTP response:

HTTP/1.1 200 OK
Date: sat, 31 Dec 2005 23:59:59 GMT
Content-Type: text/html; charset = ISO-8859-1
Content-Length: 122

<HTML>
<Head>
<Title> wrox homepage </title>
</Head>
<Body>
<! -- Body goes here -->
</Body>
</Html>

In this example, the HTTP status provided by the Status lineCodeIs 200 and the message is OK. Status lines always contain status codes and short messages to avoid confusion. The most common status codes are:

◆ 200 (OK): The resource is found and everything is normal.

◆ 304 (not modified): The resource has not been modified since the last request. This is usually used for browser caching.

◆ 401 (unauthorized ): The client does not have access to the resource. This usually requires the user to enter the user name and password in the browser to log on to the server.

◆ 403 (Forbidden ): The client is not authorized. This is usually because an incorrect user name or password is entered after 401.

◆ 404 (not found ): The requested resource does not exist at the specified location.

It is the first line after the status line. Generally, the server returns a header named data to indicate the date and time of the response generation (the server usually returns some information about itself, though not necessary ). You should be familiar with the next two headers, that is, the Content-Type and Content-Length in the post request. In this example, the header Content-Type specifies the MIME type HTML (text/html) whose encoding type is ISO-8859-1 (which is the encoding standard for American English resources ). The response body contains the HTML source file of the requested resource (although it may contain plain text or binary data of other resource types ). The browser displays the data to the user.

Note: The request type for the response is not specified here, but this is not important to the server. The client knows what type of data will be returned for each type of request and determines how to use the data.

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.