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)

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM