AJAX learning notes (1) Basic knowledge and ajax learning notes

Source: Internet
Author: User

AJAX learning notes (1) Basic knowledge and ajax learning notes
I. HTTP Protocol 1. HTTP: Rules for computer communication over the network, used by the browser to send requests to the server. 2. HTTP is a stateless protocol. Stateless means that the server does not retain any connection-related information. The browser client sends a request to the server and the server returns a response, then the connection is closed. The connection does not have any memory. For subsequent processing, if the information transmitted by the previous connection is required, it must be re-transmitted. 3. Complete HTTP requests require the following seven steps: (1) Establish a TCP connection. Generally, the HTTP protocol is connected by the TCP protocol, and the TCP protocol is reliable. (2) The Web browser sends request commands to the Web server. (3) The Web browser sends the request header information. (4) The Web server responds. (5) The Web server sends response header information. (6) The Web server sends data to the browser. (7) The Web server closes the TCP connection. 4. An HTTP request consists of four parts: (1) the HTTP request method is GET or POST. (2) The request URL, that is, the request address. (3) request headers, including client environment information and authentication information. (4) The request body, that is, the request body, contains the query string information and form information submitted by the customer. 5. GET and POST methods (1) GET: usually used to obtain information, that is, query. The URL is used to pass parameters. The parameters are generally the conditions for users to obtain information. By default, HTTP uses the GET method. For query requests, the GET method is secure, but it is insecure to use the GET method to send user names and passwords, because the information sent by the GET request is visible to anyone, the data transmitted by the GET request will be displayed in the URL path. The GET method has a limit on the number of sent messages, typically 2000 characters. (2) POST: It is generally used to modify resources on the server, such as adding, deleting, and modifying. Generally, data is transmitted to the server through a form. The transmitted data is invisible and all transmitted data is encapsulated in the Request body. 6. An HTTP response consists of three parts: (1) response code: a status code consisting of a number or text to show whether the request is successful or failed. (2) Response Header: contains server information, such as server type, date and time, content type, and length. (3) response body: the response body, that is, the returned response information. 7. HTTP response status code Introduction: 1XX: Information Class, indicating that the WEB browser request is being processed. 2XX: Successful, indicating that the user's request is correctly received, understood, and processed, for example, 200 OK. 3XX: Redirection, indicating that the request failed and the customer must take further action. 4XX: it is common that a client-submitted request has an error, for example, 404 not found, indicating that the request path does NOT exist. 5XX: Common, server error, indicating that the server cannot process the request, for example, Error 500. 2. Send requests using the XMLHttpRequest object: the most important part of AJAX is to use the XMLHttpRequest object to send requests. The XMLHttpRequest object targets IE7 and later, Firefox, Chrome, Safari, and Opera, for IE5 and IE6, ActiveXObject is used. The method for creating an object is as follows: var xmlHttpRequest; if (window. XMLHttpRequest) {// XMLHttpRequest exists, indicating IE7 and other mainstream browsers. XmlHttpRequest = new XMLHttpRequest ();} else {xmlHttpRequest = new ActiveXObject ();} the object sends requests in two ways. 1. open (method, url, async): method: POST or GET. Url: The Request Path. Async: asynchronous. true indicates asynchronous. false indicates synchronous. The default value is true. AJAX is usually asynchronous. Note: synchronous and asynchronous: Synchronous means that only one request can be executed at a time, and subsequent requests must wait until the execution of the previous request is complete. asynchronous means that multiple requests can be executed at the same time, without waiting for the execution of the previous request to complete, asynchronous requests are much more efficient than synchronous requests. 2. send (string): send a request. The string type parameter of the send (String) method is used to pass the request parameters. Because the GET method parameters are usually followed by the url, therefore, when a request is sent using the GET method, the parameters of the send (string) method can be left blank or null. The POST method cannot pass parameters through URLs, therefore, when the POST method is used to send a request, the parameters of the string type of the send (String) method are usually required. Of course, if this parameter is left blank, there is no error, but the application is rare. 3. the XMLHttpRequest. setRequestHeader () method setRequestHeader () is used to set and change the information contained in the request header when an HTTP request is sent. There are usually two parameters. The first parameter indicates the field name in the request header, and the second parameter indicates the content corresponding to the field name. For example, the original request header: GET/bb. asp? Www = 1234 HTTP/1.1 Accept: */* Accept-Language: zh-cn UA-CPU: x86 Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1 ;. net clr 1.1.4322 ;. net clr 2.0.50727) Host: www.e4j.cn: 89 Connection: Keep-Alive Cookie: % C3 % F7 % CC % EC = % B0 % CB; ASPSESSIONIDASDBSDRR = BLEDBIBBCGKBJAKJCFEJKGII call setRequestHeader () method To change the request header: xmlHttpRequest. setRequestHeader ("Content-type "," Application/x-www-form-urlencoded "); xmlHttpRequest. setRequestHeader (" Content-length ", paramsSend. length); The request header Content is: GET/bb. asp? Www = 1234 HTTP/1.1 Accept: */* Accept-Language: zh-cn UA-CPU: x86 Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1 ;. net clr 1.1.4322 ;. net clr 2.0.50727) CONTENT-TYPE: application/x-www-form-urlencoded Host: www.e4j.cn: 89 Content-length: 8 Cookie: % C3 % F7 % CC % EC = % B0 % CB; ASPSESSIONIDASDBSDRR = BLEDBIBBCGKBJAKJCFEJKGII xmlHttpRequest. setRequestHeader ("Conten T-type "," application/x-www-form-urlencoded "); sets a request header, indicating that the encoding method for the content submitted to the server text is URL encoding, there are other encoding methods, such as the CONTENT-TYPE: multipart/form-data xmlHttpRequest. setRequestHeader ("Content-length", paramsSend. length); indicates the size of the submitted data in bytes. For requests submitted using the GET method, Content-length is invalid because no data is required. The XMLHttpRequest. setRequestHeader () method must be written in the middle of the open () and send (string) methods. Otherwise, an exception is thrown. 4. XMLHttpRequest gets the response responseText: gets the response data in string form. ResponseXML: Get Response Data in XML format. Normally, the response information is returned in Json format. Status and statusText: return the HTTP status code getAllResponseHeader () in numbers and text format: Get all Response Headers getResponseHeader (String string): query the value of a field in the response. 5. listener response status readyState attribute: 0: the request is not initialized, and open is not called. 1: The server connection has been established and open has been called. 2: The request has been received and the header information has been received. 3: The response body has been received during request processing. 4: The request is complete and the response is complete. Method for listening to the response status: onreadystatechange event, which is triggered when the readyState attribute changes. var request = new XMLHttpRequest (); request. open ("GET", "get. php ", true); request. send (); request. onreadystatechange = function () {if (request. readystate === 4 & request. statue = 200) {// readystate indicates the response status, and status indicates the Request status do something;} remarks: differences between two equal signs and three equal signs in js, if two equal signs are used to compare the sizes, when the variable types on both sides of "=" are different, they are automatically converted to the same type before comparison. When three equal signs are used, if the types of variables on both sides are different, the comparison result is different. For example, var a = 0, B = '0'; alert (a = B) + '-' + (a = B )) the result is "true-false ". Specific comparison rules see link: http://zzstudy.offcn.com/archives/10937 Reference URL: MOOC Ajax full contact video: http://www.360kan.com/va/asEsaXNu8pk7ED.html XMLHttpRequest. setRequestHeader (): Http://www.cnblogs.com/s-y-l/archive/2011/11/02/2233076.html

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.