Http
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script src="easyhttp.js"></script>
<script src="app.js"></script>
</body>
</html>
app.js
const http = new EasyHttp;
// 请求数据
// http.get("http://jsonplaceholder.typicode.com/users")
// .then((data) => {
// console.log(data);
// })
// .catch(err => console.log(err))
// 传输数据
const data = {
name:"Henry",
username:"米斯特吴",
email:"27732357@qq.com"
};
// post user
// http.post("http://jsonplaceholder.typicode.com/users",data)
// .then(data => console.log(data))
// .catch(err => console.log(err));
// update user
// http.put("http://jsonplaceholder.typicode.com/users/2",data)
// .then(data => console.log(data))
// .catch(err => console.log(err));
// delete user
http.delete('http://jsonplaceholder.typicode.com/users/2')
.then(data => console.log(data))
.catch(err => console.log(err));
esayhttp
/**
* 封装fetch
* 更快,更简单的请求数据
*
* @version 2.0.0
* @author 米斯特吴
* @license MIT
*
**/
class EasyHttp{
// get
async get(url){
const response = await fetch(url);
const resData = await response.json();
return resData;
}
// post
async post(url,data){
const response = await fetch(url,{
method:"POST",
headers:{
'Content-type':'application/json'
},
body:JSON.stringify(data)
});
const resData = await response.json();
return resData;
}
// put
async put(url,data){
const response = await fetch(url,{
method:"PUT",
headers:{
'Content-type':'application/json'
},
body:JSON.stringify(data)
});
const resData = await response.json();
return resData;
}
// delete
async delete(url){
const response = await fetch(url,{
method:"DELETE",
headers:{
'Content-type':'application/json'
}
})
const resData = await "数据删除成功!";
return resData;
}
}