接口文档API、剖析http协议, 原生http请求函数

时间:2020-05-09
本文章向大家介绍接口文档API、剖析http协议, 原生http请求函数,主要包括接口文档API、剖析http协议, 原生http请求函数使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

http协议:超广本传输协议

特点:

        短连接 请求完成后就断开

         无状态  对于事务处理无记忆能力

         媒体独立 客户端要指定适合的传输内容类型,如json

http  是建立在tcp/ip协议之上的应用层协议

H5新增的: 长连接  websocket 双向通信

http主要三部分: 请求行(url),请求头(header),请求体(参数)响应头

http 1.0: get post

http 1.1 : put  delete options

restful api 风格 (细化)

get—查询,post --添加, put--修改, delete -- 删除

get:获取数据

post: 添加 修改 删除

差别:get的参数拼接在url后面,(http://www.xx.com/?name=xxx&age=20 或者 http://www.xx.com/:name)参数有长度限制 

           post参数在请求体,请求体参数:json ,formdata

           与put,delete没有差别,名字风格不同

           options --试探跨域

跨域:

         地址:协议 IP 端口

http请求:

function getXMLHttpRequest(){
            var xmlhttp;
            if(window.ActiveXObject){
                // ie5 ie6
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }else if(window.XMLHttpRequest){
                // ie7+ ff chrome 浏览器原生
                xmlhttp = new XMLHttpRequest();
            }else {
                xmlhttp = null;
            }
            return xmlhttp
        }
function getData(){
      var xmlhttp = getXMLHttpRequest();
           // true异步 false同步
           xmlhttp.open('get','http://localhost:3000/info',true);
           xmlhttp.send();//发送请求
           xmlhttp.onreadystatechange = function(){
               //200 请求成功
               if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                   console.log(xmlhttp.responseText);
               }
           }
        }

原文地址:https://www.cnblogs.com/carry-carry/p/12859166.html