<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script src="axios.js"></script>
<h1>Axios Test</h1>
<button id="btn1">Get query</button>
<button id="btn2">Post query</button>
<script>
const btn1 = document.getElementById("btn1");
const btn2 = document.getElementById("btn2");
btn1.addEventListener("click", () => {
axios({
method: "get",
url: "http://127.0.0.1:8090/students",
responseType: "json",
})
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.log(error);
});
});
btn2.addEventListener("click", () => {
axios({
method: "post",
url: "http://127.0.0.1:8090/students",
data: {
name: "tangseng",
age: 17,
gender: "男",
address: "北京",
},
})
.then((result) => {
console.log(result.data);
})
.catch((err) => {
console.log("出错了" + err);
});
});
</script>
</body>
</html>