1.http概念

3.封装的http请求 - 图1

2.http请求步骤

3.封装的http请求 - 图2

3.http请求的内容

3.1get和post

3.封装的http请求 - 图3

3.2 http响应

3.封装的http请求 - 图4
3.封装的http请求 - 图5
3.封装的http请求 - 图6

4.实例:

  1. <!-- 封装请求函数 -->
  2. <script>
  3. function ajax({
  4. method, //请求的方式"get"/"post"
  5. url, //请求地址
  6. success //回调函数
  7. }){
  8. var xhr =new XMLHttpRequest();
  9. xhr.open(method,url,true);
  10. xhr.send();
  11. xhr.onreadystatechange = function(){
  12. if(xhr.readyState ==4 && xhr.status ==200){
  13. var res = JSON.parse(this.responseText);
  14. success(res);
  15. }
  16. }
  17. }
  18. </script>
  19. <!-- 使用函数 -->
  20. <script>
  21. ajax({
  22. method:"get",
  23. url:"http://192.168.4.18:8000/",
  24. success: function(res) {
  25. console.log(res)
  26. }
  27. })
  28. </script>