ES6 fetch方法封装

   // 请求路径 
  let url = 'http://jsonplaceholder.typicode.com/users' // 传输数据参数 const dataName = { name: "Sara", username: "高大丫", email: "35565451@qq.com" }; //封装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