Ajax combat: Sending requests to the server

Source: Internet
Author: User
Tags character set header http request

Sending requests to the server via the XMLHttpRequest object is a fairly straightforward thing to do. All we need to do is send it a URL to the server page that will generate the data. Just like the following:

XMLHttpRequest supports a large number of HTTP semantics, including optional query string parameters used to dynamically generate pages (you may already know these CGI parameters, form parameters, or ServletRequest parameters, depending on the server-side development background). Before examining how the requesting object supports these features, let's take a quick look at the basics of HTTP.

HTTP Quick Start

HTTP is so ubiquitous to the internet that we often ignore it. When writing a traditional Web application, the place where we approach the HTTP protocol in close proximity is usually to define a hyperlink or set the method property for a form. And for Ajax, we can drill down into the underlying details of those protocols, which allows us to do something amazing.

The HTTP transaction between the browser and the Web server includes a request from the browser and a response from the subsequent server (which also includes the clever, exciting code written by the web Developer, no doubt). Requests and responses are essentially text streams, and the client and the server interpret them as the primary information and the main body part followed. You can think of the first information as the address bar written on the envelope, and the body part is the letter in the envelope. The header information simply indicates how the receiver should handle the contents of the letter.

An HTTP request consists primarily of the header information and the body part that may contain some data or parameters. The response typically contains the HTML markup that returns the page. The Mozilla browser contains a useful tool called livehttpheaders (see the "Resources" section of this chapter and appendix A). Let's look at the contents of the header information for those requests and responses when the browser is working. Open Google's homepage and see what's going on at the bottom.

The first request that we send contains the header information:

GET / HTTP/1.1
  Host: www.google.com
  User-Agent: Mozilla/5.0
  (Windows; U; Windows NT 5.0; en-US; rv:1.7)
  Gecko/20040803 Firefox/0.9.3
  Accept: text/xml,application/xml,
  application/xhtml+xml,text/html;q=0.9,
  text/plain;q=0.8,image/png,*/*;q=0.5
  Accept-Language: en-us,en;q=0.5
  Accept-Encoding: gzip,deflate
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  Keep-Alive: 300
  Connection: keep-alive
  Cookie: PREF=ID=cabd38877dc0b6a1:TM=1116601572
  :LM=1116601572:S=GD3SsQk3v0adtSBP

The first line tells us which HTTP method to use. Most web developers are familiar with GET, which is used to obtain documents, and post, which is used to submit HTML forms. The World Wide Web Consortium's statute also includes a number of other common methods, including head, to obtain the first information of a file; Put, used to upload documents to the server, delete, to remove documents on the server. Subsequent header information is used for communication, and the client tells the server what type of content it can support, the character set, and so on. Because I've visited Google before, it sends a cookie, and this brief message tells Google who I am.

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.