export default ({ url, method = 'GET', data = null, }) => { // Request configuration let options = { method } // When data is not empty, it is a post request if (data) { options = { ...options, body: JSON.stringify(data), headers: { 'content-type': 'application/json' } } } return fetch(url, options) .then(res => res.json()) .then(data => data) } usegetpost<script type="module"> import fetchApi from './js/fetch.js' const vm = new Vue({ el: '#app', data: { users: [] }, // Initiate 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)) } }) </script> The above is the detailed content of the detailed explanation of the fetch network request encapsulation example. For more information about fetch network request encapsulation, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: MySQL Series 12 Backup and Recovery
>>: The webpage cannot be opened because the div element lacks a closing tag
Table of contents 1. Installation 1. Introduction...
I installed a virtual machine a long time ago, an...
The project needs to use MySQL. Since I had alway...
The <TH> tag is used to set the properties ...
We often see a cool effect where the mouse hovers...
Table of contents Overview Solution 1: Closures S...
Get the Dockerfile from the Docker image docker h...
Prepare the database (MySQL). If you already have...
What is k3d? k3d is a small program for running a...
Click here to return to the 123WORDPRESS.COM HTML ...
Table of contents Prefab How to create a prefab T...
Preface In the Linux operating system, device fil...
The growth path from a Linux novice to a Linux ma...
Deploy database based on docker sudo docker pull ...
Preface Hello everyone, I am Liang Xu. At work, w...