// 請求路徑
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)