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
About derived tables When the main query contains...
Implementing responsive layout with CSS Responsiv...
1. Server environment configuration: 1. Check dis...
JavaScript writes a random roll call webpage for ...
Recently, when I was writing web pages with PHP, I...
mysql master-slave configuration 1. Preparation H...
Table of contents Preface question principle test...
Table of contents Preface Error Object throw try…...
In the UI cutting process, the page is often comp...
Add secure_file_priv = ' '; then run cmd ...
Cause of the problem: At first, the default yum s...
Copy code The code is as follows: <!DOCTYPE ht...
Preface: In the daily use of the database, it is ...
Table of contents 1. Event Flow 1. Concept 2. DOM...
Preface Readers who are familiar with MySQL may f...