• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    Ajax常用封装库——Axios的使用

    Axios 是目前应用最为广泛的 AJAX 封装库

    Axios的特性有:

    使用axios时,需要通过使用script标签引入:https://unpkg.com/axios/dist/axios.min.js
    axios的中文网链接:Axios中文网

    Axios API

    向axios()传递相关配置来创建请求;

    常用的配置项

    axios().then(function(response){
     //正常请求的响应信息对象response
    })
    .catch(function(error){
     //捕获的错误
    })

    代码展示如下:

    script src="https://unpkg.com/axios/dist/axios.min.js">/script>
    script>
     //使用axios方法    post请求
    axios({
             url:"/pinglun",
             method:"post",
             baseURL:"http://localhost:3000",
             header:{
                   "Content-Type":"application/json"
             },
            data:{
                "content":"well",
                "lyId":4
             },
        timeout:1000,
      }).then(function(res){
           console.log(res.data);
       }).catch(function(error){
           console.log(error);
    })
     /script>

    axios 全局默认值的配置

    axios.defaults.baseURL = 'https://xxx.xxx.com';
    axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencode'

    axios拦截器:在请求或响应被then或catch处理前拦截它们

    axios 的请求拦截器

    //axios 的请求拦截器
    axios.interceptors.request.use(function(config){
     //配置项config
      config.params = {
            id:2 //对配置项中的params进行更改,筛选id=2
        }
       return config;//要有返回值
    })
        
    //axios 方法
    axios("http://localhost:3000/liuyan")
    .then(function(res){
          console.log(res.data);
     })
    .catch(function(error){
          console.log(error);
    })
        
    //axios 方法
    axios("http://localhost:3000/pinglun")
    .then(function (res) {
        console.log(res.data);
    })
    .catch(function (error) {
         console.log(error);
    })
    //多个axios方法也可以拦截

    axios 的响应拦截器

    //axios 的响应拦截器
    axios.interceptors.response.use(function(response){
         return(response.data);//response里有很多值,选择data即可
    })
        
    //axios 方法
    axios("http://localhost:3000/liuyan")
    .then(function (res) {
          console.log(res);//response那里拦截了,已经将数据传成data了
    })
    .catch(function (error) {
         console.log(error);
    })

    axios的快速请求方法

     axios.get(url[,config])

    //axios.get(url[,config])
        
    axios.get("http://localhost:3000/liuyan?id=2")
     .then(function(res){
         console.log(res.data);
    })
        
    axios.get("http://localhost:3000/liuyan",{
       params:{
            id:1
       }
    }).then(function(res){
        console.log(res.data);
    })

     axios.post(url[,data[,config]])

    //axios.post(url[,data[,config]]) , post请求,添加数据
    axios.post("http://localhost:3000/users",{
        name:"laowang",
        age:23,
        class:1
    })

     axios.delete(url[,config])

    //axios.delete(url[,config])
    axios.delete("http://localhost:3000/liuyan",{
       params:{
             id:5
        }
    })

     axios.put(url[,data[,config]])

    //axios.put(url[,data[,config]])
    axios.put("http://localhost:3000/liuyan",{
        name:"wangshisan",
        id:11
    })

    XMLHttpRequest2.0,html5对XMLHttpRequest类型全面升级,使其变得更加易用、强大。

    onload / onprogress

      XML.onload 事件:只在请求完成时触发

      XML.onprogress 事件:只在请求进行中触发

    //xhr.onload事件:只在请求完成时触发
    //xhr.onprogress事件:只在请求进行中触发
    var xhr = new XMLHttpRequest();
    xhr.open("get","http://localhost:3000/pinglun");
    xhr.onload = function(){
         console.log("load:",this.readyState);
    };
    xhr.onprogress = function(e){
        console.log("progress:",this.readyState);
        //在周期性请求过程中,接收到的数据个数
         console.log(e.loaded);
         //接收数据的总个数
         console.log(e.total);
    }
    xhr.send(null);

    response属性

      以对象的形式表述响应体,其类型取决于responseType的值。根据responseType的值,来通过特定的类型请求数据。

      responseType要在调用open()初始化请求之后,在调用send()发送请求到服务器之前设置才会有效。

    //XMLHttpRequest之前的response返回
    //responseText
    // responseXml
    var xhr = new XMLHttpRequest();
    xhr.open("get","http://localhost:3000/pinglun");
    xhr.onload = function(){
      var data = JSON.parse(this.responseText);
              console.log(data);
       }
    xhr.send(null);
               
    // xhr2.0新增的response属性 
    // response
    // responseType
    var xhr = new XMLHttpRequest();
    xhr.open("get","http://localhost:3000/liuyan");
    xhr.responseType = "json";
    xhr.onload = function(){
        console.log(this.response);
    }
    xhr.send(null)

    以上就是Ajax常用封装库——Axios的使用的详细内容,更多关于Ajax封装库Axios的使用的资料请关注脚本之家其它相关文章!

    您可能感兴趣的文章:
    • Vue CLI项目 axios模块前后端交互的使用(类似ajax提交)
    • Vue官方推荐AJAX组件axios.js使用方法详解与API
    • vue项目使用axios发送请求让ajax请求头部携带cookie的方法
    • vue 组件的封装之基于axios的ajax请求方法
    • vue结合axios与后端进行ajax交互的方法
    • 关于vue中的ajax请求和axios包问题
    • vue axios 在页面切换时中断请求方法 ajax
    • axios进阶实践之利用最优雅的方式写ajax请求
    • 关于前端ajax请求的优雅方案(http客户端为axios)
    • 在Vue组件化中利用axios处理ajax请求的使用方法
    • vue使用Axios做ajax请求详解
    • VUE 更好的 ajax 上传处理 axios.js实现代码
    上一篇:如何封装一个Ajax函数
    下一篇:ztree+ajax实现文件树下载功能
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    Ajax常用封装库——Axios的使用 Ajax,常用,封装,库,Axios,的,