JS Learning Summary----HTTP request mode and synchronous programming and asynchronous programming in Ajax

Source: Internet
Author: User

First, the request method

varXHR =createxhr (); Xhr.open ("Get", "/getlist?num=12",true); Xhr.open ("Post", "/getlist",true); Xhr.send (' {' name ': ' Zhangsan ', ' Age ': 7} ')//HTTP method; How clients send requests to the server side//the following request method regardless of which can pass data to the server, you can also get data from the server, in the essence of the following way is not any difference, such as post-do things with Get can also be done, and we will talk about the difference is the current developers of the conventional norms: /*get: Generally used to get data from the server (less data to the server, more data from the server, the most common request in the project is get) use GET request to the server to pass content we generally use the "URL question mark Way" POST: generally applied to the server Push data (much to the server, less from the server) use a POST request to deliver content to the server we generally use the "request principal way" to pass to the server put: generally used to add resources to the server file (we upload the image function) DELETE: Generally applied to the slave server Delete resource file on head: generally applied to get the server's response header information only*//*get PK POST size issue: The content of a GET request is passed to the server with a limit of size, while POST theoretically has no reason for limiting: get the URL to the server, and each browser for the length of the URL limit, Google 8kb, fire Fox 7kb, ie12kb length limit, if the length of the URL exceeds the limit, the browser will be the part of the cache to intercept the problem: The GET request will be cached (this cache is not necessarily 304), post is not cached in the project our GET request is generally not let it appear    Cache "Clear Cache": Add a random number xhr.open at the end of the URL ("Get", "/getlist?num=12&_=" +math.random (), true); Security issues generally get unsafe, while post is relatively secure*/

Second, synchronous programming and asynchronous programming

//Synchronous (Sync) and asynchronous (async) Programming//JS is a single thread, we'd better use asynchronous programming//timers are programmed asynchronously.//event bindings are asynchronous programming.//callback functions can also be understood as asynchronous programming//asynchronous programming can be used in AjaxvarCount = 0; Window.setinterval (function() {count++; Console.log (count);//re-output 1},1000)//If the timer is set to 0 is not executed immediately, but it needs to wait for a period of time, we set the time is 1000ms, but not a certain amount of time to execute, if the current browser thread is occupied, will always waitConsole.log (count);//First output 0 while(1){}//the code inside the timer will not execute after adding a while//If the time of multiple timers is reached, first execute the timer with a short time//Synchronization in Ajax: When the AJAX task starts (xhr.send) waits for readystate===4 to perform the following tasks//Async in Ajax: When the AJAX task starts (Xhr.send) does not need to wait until readystate===4, we can continue to do other tasks, and only after the other tasks are completed, we see whether it is 4, to reach 4 when we do some related operations 

JS Learning Summary----HTTP request mode and synchronous programming and asynchronous programming in Ajax

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.