12_axios的默认配置.html 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Axios的配置对象</title>
  8. <!-- 引入axios -->
  9. <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  10. </head>
  11. <body>
  12. <button id="btn1">按钮1</button>
  13. <!-- <button id="btn2">按钮2</button> -->
  14. <script>
  15. axios.defaults.baseURL = "http://localhost:3000"
  16. axios.defaults.headers.common['Authorization'] = `Bearer ${localStorage.getItem("token")}`;
  17. document.getElementById("btn1").onclick = () =>{
  18. //直接调用axios发送请求
  19. //axios(config)
  20. axios({
  21. url:"students",
  22. method:"post",
  23. data:{
  24. name:"唐僧",
  25. age:18,
  26. gender:"男",
  27. address:"女儿国"
  28. },
  29. timeout:1000,
  30. }).then((res) =>{
  31. //res是axios封装过
  32. //console.log(res);
  33. console.log(res.data);
  34. }).catch((err) => {
  35. console.log("出错了",err);
  36. })
  37. }
  38. </script>
  39. </body>
  40. </html>