Brief analysis of Ajax:get and post

Source: Internet
Author: User
Tags data structures
asynchronous Javascript and XMLAsynchronous JavaScript and XML: Interacting with the server, reading the file
Implementation of the class used: XMLHttpRequest: Incompatible with IE5, IE6; ActiveXObject: Compatible with IE5, IE6;
Client server communication is through the HTTP protocol to communicate, HTTP has two common methods (Get/post): 1.GET: Parameters to the URL, use the interface address is separated from the parameters, multiple parameters use & separate. 2.POST: Data transfer to body inside; The transmitted information is relatively safe; the transmitted data is not limited in size. (not used) 3.DELETE 4.PUT
JS sends a request in a Get mode;
var req = new XMLHttpRequest (); Req.open ("GET", "xxx"); Req.send (); Req.onreadystatechange = function () {alert (this.readystate);};
JS in the receiving service sent to the client content XMLHttpRequest > Response response

synchronization: A line of code execution, a more time-consuming task, will wait for the time-consuming task to complete after the execution continue execution (a time-consuming task program will get stuck until the task is completed); XMLHttpRequest is asynchronous by default; Async: the meaning of open three parameters: XMLHttpRequest->open (requester of HTTP method, the requested address, synchronous or asynchronous)



HTTP protocol Transfer contentTwo methods: GET: The parameters of the transfer are directly stitched into the URL, and the same as the HTML direct value, need to use. To separate the passed arguments, if there are multiple parameters, use & to separate each parameter; get features: Get parameters in the URL can not be passed the larger parameters (because the size of the URL), the parameters are exposed in the URL;
Get transfer speed is a bit faster--as long as it does not involve the transfer of large files, passwords, more important, are used to get; POST: The transfer parameters are placed in the body body, the size of the transmission is not limited;
To interact with the server, you need to know what to interact with and what to know 1. The location of the requested data is where the host address (IP or domain name) needs to specify the location of the specific file 2. Which HTTP method is required to use which API the service provider needs to use, 3. Parameters to transfer; get: Stitching to URLs, using. Stitching, using & separating POST: The request parameter is transmitted in a different part of the HTTP header (named entity body), which is used to transfer the form information, so the Content-type must be set to: application/ X-www-form-urlencoded. The post is designed to support user fields on Web Forms, and its parameters are also transmitted as key/value. However: it does not support complex data types, because post does not define the semantics and rules for transferring data structures. Some sample code:
When you do not know what type the value is,//can be assigned by NULL;
var result = null; var request = new XMLHttpRequest ();
To set the request//Set HTTP method of the request GET//Set the requested server location: Http://route.showapi.com/255-1 request.open ("GET", "http://route.showapi.com /255-1 "); Request.send ();
Monitor the transfer status of the client transmitting data to the service side

Request.onreadystatechange = function () {
ReadyState: Read the status of the transmission Console.log (this.readystate);
if (This.readystate ===xmlhttprequest.done) {//response HTTP response, there may be a value we need

Alert ("Interactive completion"); result = Request.response; } }; Request.send (); Console.log (result);

--------------------------------------------------------------------------------------------------------------- ----
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.