Form form------AJAX------Fetch

Source: Internet
Author: User

1. Before Ajax

At the beginning, the browser initiates the request as follows:

The user enters the URL/a label/img label, etc.--the server returns HTML/CSS/JS--and reloads the page after the user receives it.

The above request initiation method either causes the page to refresh (and so is the form submission), or can only request a specific type of file (picture, CSS, or JS).

The arrival of the 2.AJAX

The essence of Ajax is to use JS to initiate a request, and get the content returned by the server.

The key to JS initiating the request is the XMLHttpRequest interface provided by the browser. The request process is as follows:

//Create a XMLHttpRequest objectvarRequest =NewXMLHttpRequest ()//Monitor the status change after successful request, Request.responsetext is the content returned by the server (the default is the string)Request.onreadystatechange =function() {  if( This. readyState = = 4 && This. Status = = 200) {Console.log (Request.responsetext)}};//Set Request ParametersRequest.open ("GET", "filename",true);//Send RequestRequest.send ();

The above process, through the encapsulation of jquery:

$.get (' filename '). Then (function(response) {  //  response here is the contents of the return})

3.Fetch API

Now that the browser has XMLHttpRequest this API, why not just provide the encapsulated interface?

The Fetch API is the native AJAX interface provided by the browser. You can use the Window.fetch function instead of the previous $.ajax.

In other words, the browser helps us to implement the Jquery.ajax, and we can send the asynchronous request directly with Fetch.

Fetch ('/'). Then (function(response) {    response.text (). Then (function( Text) {        console.log (text)})    })

The fetch API, however, is based on ES6.

Form form------AJAX------Fetch

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.