当前位置:   article > 正文

ajax请求——XMLHttpRequest请求

ajax请求——XMLHttpRequest请求

 个人练习笔记-----Ajax01

一、GET

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <div id="dom">内容</div>
  10. <button onclick="getData()">发送请求</button>
  11. <script>
  12. //目前前后端数据请求,使用的都是ajax技术(网络请求技术),其中原生Ajax就是XMLHttpRequest,其次衍生的更加方便好用的Ajax有:fetch、axios
  13. function getData(){
  14. // 1.先创建XHR对象
  15. var xhr=new XMLHttpRequest();
  16. // 2.通过open方法,配置请求
  17. xhr.open("GET","https://v5.crmeb.net/api/groom/list/3?page=1&limit=9");
  18. // 5.添加请求头setRequestHeader() Content-Type:用于设置请求参数的格式,设置 “?参数” 或者 “‘请求体’参数” ,以什么样的格式传递给服务器
  19. // setRequestHeader()位置要放在open()和send()中间
  20. xhr.setRequestHeader("Content-Type","application/json");
  21. // setRequestHeader("Authori-Zation","...")用户登录身份(加密过的)
  22. xhr.setRequestHeader("Authori-Zation","Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwd2QiOiJkNDFkOGNkOThmMDBiMjA0ZTk4MDA5OThlY2Y4NDI3ZSIsImlzcyI6InY1LmNybWViLm5ldCIsImF1ZCI6InY1LmNybWViLm5ldCIsImlhdCI6MTcwMzQ4ODk3MCwibmJmIjoxNzAzNDg4OTcwLCJleHAiOjE3MDYwODA5NzAsImp0aSI6eyJpZCI6NTA2NzYsInR5cGUiOiJhcGkifX0.HAvGkn0unJ6XvBjC_fp5WOkCPOu7qlNxwedmJAF5wOk")
  23. // 3.将这个接口请求,发送至服务器
  24. xhr.send();
  25. // 4.监听请求状态 readyStatus的变化,就会触发 onreadystatechange 事件
  26. xhr.onreadystatechange=()=>{
  27. if(xhr.readyState===4 && xhr.status ===200){
  28. // 请求成功,可以接收数据,responseText:以字符串形式返回服务器数据,需要转化为JSON对象
  29. const data=JSON.parse(xhr.responseText);
  30. console.log(data);
  31. }
  32. }
  33. }
  34. </script>
  35. </body>
  36. </html>

 https://v5.crmeb.net/api/groom/list/3?page=1&limit=9  ------协议:// 域名 / 地址?参数

1.xhr.open("GET","https://v5.crmeb.net/api/groom/list/3?page=1&limit=9");对应的参数从下图中所示中获取

2.xhr.setRequestHeader("Content-Type","application/json");//验证身份

xhr.setRequestHeader("Authori-Zation","Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwd2QiOiJkNDFkOGNkOThmMDBiMjA0ZTk4MDA5OThlY2Y4NDI3ZSIsImlzcyI6InY1LmNybWViLm5ldCIsImF1ZCI6InY1LmNybWViLm5ldCIsImlhdCI6MTcwMzQ4ODk3MCwibmJmIjoxNzAzNDg4OTcwLCJleHAiOjE3MDYwODA5NzAsImp0aSI6eyJpZCI6NTA2NzYsInR5cGUiOiJhcGkifX0.HAvGkn0unJ6XvBjC_fp5WOkCPOu7qlNxwedmJAF5wOk")

在请求头中查找 ,具体解释在开头代码中

Content-Type : application/json;//字符串(参数)格式

二、POST

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <button onclick="getNum()">购物车+1</button>
  10. <script>
  11. function getNum(){
  12. // 1.先创建
  13. let xhr=new XMLHttpRequest();
  14. // 2.打开open()
  15. xhr.open("POST","https://v5.crmeb.net/api/cart/num");
  16. xhr.setRequestHeader("Content-Type","application/json")
  17. xhr.setRequestHeader("Authori-Zation","Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwd2QiOiJkNDFkOGNkOThmMDBiMjA0ZTk4MDA5OThlY2Y4NDI3ZSIsImlzcyI6InY1LmNybWViLm5ldCIsImF1ZCI6InY1LmNybWViLm5ldCIsImlhdCI6MTcwMzQ4ODk3MCwibmJmIjoxNzAzNDg4OTcwLCJleHAiOjE3MDYwODA5NzAsImp0aSI6eyJpZCI6NTA2NzYsInR5cGUiOiJhcGkifX0.HAvGkn0unJ6XvBjC_fp5WOkCPOu7qlNxwedmJAF5wOk");
  18. // 3.发送send()
  19. xhr.send(JSON.stringify({id:4821,number:7}));
  20. // 4.监听请求状态onreadystatechange
  21. xhr.onreadystatechange=()=>{
  22. if (xhr.readyState === 4 && xhr.status === 200) {
  23. const data = JSON.parse(xhr.responseText);
  24. console.log(data);
  25. }
  26. }
  27. }
  28. </script>
  29. </body>
  30. </html>

发送请求

xhr.send(JSON.stringify({id:4821,number:7}));//携带的参数

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/102183
推荐阅读
相关标签
  

闽ICP备14008679号