代码:
<!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 rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/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 btn = document.querySelectorAll('.btn')
axios.defaults.method = 'get'
axios.defaults.baseURL = 'http://localhost:3000'
btn[0].onclick = function(){
axios({
url: '/posts'
})
.then(response => {
console.log(response)
})
}
</script>
</body>
</html>