<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link
crossorigin="anonymous"
href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css"
rel="stylesheet"
/>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.6/axios.min.js"></script>
</head>
<body>
<div class="container">
<h2 class="page-header">基本使用</h2>
<button class="btn btn-primary">发送GET请求</button>
<button class="btn btn-warning">发送POST请求</button>
<button class="btn btn-success">发送PUT请求</button>
<button class="btn btn-danger">发送DELETE请求</button>
</div>
<script>
const btns = document.querySelectorAll('button');
btns[0].onclick = function () {
axios({
method: 'GET',
url: 'http://localhost:3000/posts/1',
}).then(v => {
console.log(v);
});
};
btns[1].onclick = function () {
axios({
method: 'POST',
url: 'http://localhost:3000/posts',
data: {
titie: '今天天气不错',
author: '张三',
},
}).then(v => {
console.log(v);
});
};
btns[2].onclick = function () {
axios({
method: 'PUT',
url: 'http://localhost:3000/posts/2',
data: {
titie: '今天天气不错',
author: '李四',
},
}).then(v => {
console.log(v);
});
};
btns[3].onclick = function () {
axios({
method: 'DELETE',
url: 'http://localhost:3000/posts/3',
}).then(v => {
console.log(v);
});
};
</script>
</body>
</html>