Vue network request scheme native network request and js network request library

Vue network request scheme native network request and js network request library

1. Native network request

1. XMLHttpRequest (w3c standard)

// The product when there is no promise

At that time, everything was called back, which was too troublesome.

2. Fetch

//Object provided by html5, based on promise. Because of the existence of promise, it is used to simplify network requests.

Using Fetch - Web API Reference | MDN

Fetch is a new ajax solution Fetch returns a Promise object. Fetch is not a further encapsulation of ajax, but native js, and does not use the XMLHttpRequest object.

parameter:

1. The first parameter is the URL:

2. The second parameter of the setting request is an optional parameter

3. Return using Promise to handle results/callbacks

fetch(url, options).then(res=>res.json()/text()).then(ret=>console.log(ret))

Compatibility issues:

What should I do if the lower version of IE is not compatible with Fetch? =》Use the third-party Fetch library [fetch-polyfill]

Use fetch to make a network request. Let url1 url2 execute both addresses at the same time before performing related operations. Promise.all

let url1 I have to handle Promise.finally regardless of whether it is executed successfully

fetch encapsulates network requests

2. js network request library

axios

Returns json data as promise type.

Documentation: Instructions · Axios Chinese Instructions · Kanyun

Axios is a promise-based HTTP library that can be used in browsers and node.js.

Can intercept requests and responses and automatically convert JSON data. Axios is also the network request library recommended by the Vue author.

// axios.get/post/put/delete
axios.get(url,config) // config can set header information axios.post(url,data,config)
axios.put(url,data,config)
axios.delete(url,data,config) 

Or request via instance

Add setting information (common) through instance request!

Do not set it uniformly across instances (not commonly used) because it is set for everyone

// Set axios.defaults.timeout = 10000 for axios
axios.defaults.baseURL = 'http://localhost:3000'
axios.defaults.headers.a = 'admin'

postSubmit

// post submission axios.post(url, { id: 1, name: 'aaaa' }).then(res => console.log(res.data))

Or axios directly uses # standard writing (not commonly used)

// axios directly uses axios({
  url,
  method:'get',
  data:{}
}).then(res => console.log(res))

Interceptor Aspect Programming

(Pipeline) (Middleware)

1. Request interceptor (can be called multiple times)

axios.interceptors.request.use(config => {
  // Unified setting of request domain name address config.baseURL = 'http://localhost:3000'
  // Timeout config.timeout = 1000
  // Set header information config.headers.token = 'mytoken login'
  return config;
}, err => Promise.reject(err))

2. Response interceptor (processing, filtering)

axios.interceptors.response.use(response => {
  return response.data
}, err => {
  // You can handle this uniformly in the response interceptor, request exception alert ('Request failed, please request again')
  return Promise.reject(err)
});

The above is the details of the native network request and js network request library of the network request solution. For more information about native network requests and js network request library, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • 3 common methods of js cross-domain request data
  • Packaging and filling the gaps in WeChat applet network requests
  • Sample code for axios request network interface encapsulation in vue project
  • Detailed explanation of Vue sending ajax request
  • A brief discussion on the practical application of interceptors of Vue network requests
  • Three ways of requesting data in Vue

<<:  MySQL Series 11 Logging

>>:  Example code for converting html table data to Json format

Recommend

Use of Linux telnet command

1. Introduction The telnet command is used to log...

Getting Started Tutorial for Beginners ⑨: How to Build a Portal Website

Moreover, an article website built with a blog pro...

Example of Html shielding right-click menu and left-click typing function

Disable right-click menu <body oncontextmenu=s...

Basic understanding and use of HTML select option

Detailed explanation of HTML (select option) in ja...

Solution to the problem of var in for loop

Preface var is a way to declare variables in ES5....

How to build a redis cluster using docker

Table of contents 1. Create a redis docker base i...

Example of horizontal arrangement of li tags in HTMl

Most navigation bars are arranged horizontally as...

Methods and steps to build nginx file server based on docker

1. Create a new configuration file docker_nginx.c...

MySQL 5.7 deployment and remote access configuration under Linux

Preface: Recently I am going to team up with my p...

Drop-down menu implemented by HTML+CSS3+JS

Achieve results html <div class="containe...

Calling Baidu Map to obtain longitude and latitude in Vue

In the project, it is necessary to obtain the lat...

Teach you the detailed process of installing DOClever with Docker Compose

Table of contents 1. What is Docker Compose and h...

Detailed explanation of top command output in Linux

Preface I believe everyone has used the top comma...

Significantly optimize the size of PNG images with CSS mask (recommended)

This article is welcome to be shared and aggregat...