axios
下载json-server
npm install -g json-server
Start JSON Server json-server --watch db.json
<body>
<button id="btn">get请求</button>
<button id="btn2">post请求</button>
<button id="btn3">get请求</button>
<button id="btn4">get请求</button>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.js"></script>
<script>
document.getElementById("btn").onclick = function () {
axios({
// 请求类型
method: "get",
// url
url: "http://localhost:3000/posts/2",
}).then(function (value) {
console.log(value);
}, function (reason) {
console.log(reason);
})
}
document.getElementById("btn2").onclick = function () {
axios({
// 请求类型
method: "post",
// url
url: "http://localhost:3000/posts",
// 设置请求体
data: {
title: "今年天气不错",
author: "张三",
}
}).then(function (value) {
console.log(value);
});
}
</script>
</body>