1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <!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>Axios</title>
- <!-- 引入axios -->
- <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
- </head>
- <body>
- <button id="btn1">按钮1</button>
- <button id="btn2">按钮2</button>
- <script>
- //console.log(axios);
- document.getElementById("btn1").onclick = () =>{
- //直接调用axios发送请求
- //axios(config)
- axios({
- method:"post",
- url:"http://localhost:3000/students",
- data:{
- name:"唐僧",
- age:18,
- gender:"男",
- address:"女儿国"
- }
- }).then((res) =>{
- //res是axios封装过
- //console.log(res);
- console.log(res.data);
- }).catch((err) => {
- console.log("出错了",err);
- })
- }
- document.getElementById("btn2").onclick = () =>{
- //直接调用axios发送请求
- //axios(config)
- axios({
- method:"get",
- url:"http://localhost:3000/students",
-
- }).then((res) =>{
- //axios默认只会在响应状态码为2xx时才会调用then
- //res是axios封装过
- //console.log(res);
- console.log(res.data);
- }).catch((err) => {
- console.log("出错了",err);
- })
- }
- </script>
-
- </body>
- </html>
|