// 请求路径 let url = ‘http://jsonplaceholder.typicode.com/users‘ // 传输数据参数 const dataName = { name: "Sara", username: "高大丫", email: "[email protected]" }; //封装fetch请求数据方法 class classFetch { // fetchFun(请求路径,请求方法,传输数据参数) fetchFun(url, meth, val) { return new Promise((resolve, reject) => { fetch(url, { method: meth, headers: { ‘Content-type‘: ‘application/json‘ }, body: JSON.stringify(val) }) .then(response => response.json()) .then(data => resolve(data)) .catch(err => reject(err)) }) } } const fetchObj = new classFetch() fetchObj.fetchFun(url, ‘POST‘, dataName)
原文地址:https://www.cnblogs.com/gqx-html/p/11288955.html
时间: 2024-11-18 12:51:06