Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of fetch Network request Encapsulation

Shulou Source: shulou.com Published: 2022-06-02 11:45:22 09月10日 Update

This article focuses on "fetch network request encapsulation example analysis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "fetch network request encapsulation example analysis"!

Export default ({url, method = 'GET', data = null,}) = > {/ / request configuration let options = {method} / / data is not empty It is that post requests if (data) {options = {... options, body: JSON.stringify (data), headers: {'content-type':' application/json'}} return fetch (url, options). Then (res = > res.json ()). Then (data = > data)} use get

Post import fetchApi from'. / js/fetch.js' const vm = new Vue ({el:'# app', data: {users: []}, / / initiate a network request mounted () {let url = 'http://localhost:3000/api/users' / / fetchApi ({url}). Then (data = > console.log (data)) fetchApi ({url, method:' POST', data: {id: 200) Name: 'aaa'}}) .then (data = > console.log (data))}}) so far I believe that you have a deeper understanding of the "fetch network request encapsulation example analysis", might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Network example analysis encapsulation content learning practicality deeper interest practicality that is simple operation method more friends website channel query configuration Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Tech Info Linux Microsoft Xiaomi