json-server
npm install -g json-server
json-server --watch test.json
axios
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
<style>
.show{
padding-left: 30px;
padding-top: 30px;
position: absolute;
top:40px;
left:50px;
width: 500px;
height: 300px;
background: #f1f1f1;
}
</style>
</head>
<body>
<div class="show">
<button onclick="testGet()">GET请求</button>
<button onclick="testPost()">POST请求</button>
<button onclick="testPut()">PUT请求</button>
<button onclick="testDelete()">DELETE请求</button>
</div>
</body>
<script>
function testGet() {
axios.get('http://localhost:3000/posts')
.then(response =>{
console.log('/posts',response.data)
})
}
</script>
</html>
完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
<style>
.show{
padding-left: 30px;
padding-top: 30px;
position: absolute;
top:40px;
left:50px;
width: 500px;
height: 300px;
background: #f1f1f1;
}
</style>
</head>
<body>
<div class="show">
<button onclick="testGet()">GET请求</button>
<button onclick="testPost()">POST请求</button>
<button onclick="testPut()">PUT请求</button>
<button onclick="testDelete()">DELETE请求</button>
</div>
</body>
<script>
function testGet() {
axios.get('http://localhost:3000/posts')
.then(response =>{
console.log('/posts get',response.data)
})
}
function testPost() {
axios.post('http://localhost:3000/posts',{ "title": "明天你好", "author": "hangover" })
.then(response =>{
console.log('/posts post',response.data)
})
}
function testPut() {
axios.put('http://localhost:3000/posts/1',{
"title": "明天你好",
"author": "hangover",
},)
.then(response =>{
console.log('/posts put',response.data)
})
}
function testDelete() {
axios.delete('http://localhost:3000/posts/3')
.then(response =>{
console.log('/posts delete',response.data)
})
}
</script>
</html>