<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
class EasyHttp{
async get(url){
const response = await fetch(url)
const resData = await response.json()
return resData
}
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
}
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
}
async delete(url, data){
const response = fetch(url, {
method:"DELETE",
headers:{
'Content-type': 'application/json'
}
})
const resData = await "資料刪除成功"
return resData
}
}
const http = new EasyHttp;
http.get('http://jsonplaceholder.typicode.com/users')
.then((data) => {
console.log(data)
})
.catch(err => console.log(err))
const data = {
name:"xiangming",
username:"小明",
email:"1231231@qq.com"
}
http.post('http://jsonplaceholder.typicode.com/users',data)
.then((data) => console.log(data))
.catch(err => console.log(err))
http.put('http://jsonplaceholder.typicode.com/users/2',data)
.then((data) => console.log(data))
.catch(err => console.log(err))
http.delete('http://jsonplaceholder.typicode.com/users/2')
.then((data) => console.log(data))
.catch(err => console.log(err))
</script>
</head>
<body>
</body>
</html>