• axios请求



    axios.get('/user?ID=12345') .then(function (response) { console.log(response); console.log(response.data); console.log(response.status); console.log(response.statusText); console.log(response.headers); console.log(response.config); }) .catch(function (error) { console.log(error); });

      

    // 发送 POST 请求
    axios({
      method: 'post',
      url: '/user/12345',
      data: {
        firstName: 'Fred',
        lastName: 'Flintstone'
      }
    });
    
    axios.post('/user', {
        firstName: 'Fred',
        lastName: 'Flintstone'
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });

    执行多个并发请求

    function getUserAccount() {
      return axios.get('/user/12345');
    }
    
    function getUserPermissions() {
      return axios.get('/user/12345/permissions');
    }
    
    axios.all([getUserAccount(), getUserPermissions()])
      .then(axios.spread(function (acct, perms) {
        // 两个请求现在都执行完成
      }));
    

     封装请求:

    function ajax_post(url,data,that,callback){
        axios({
            method:"POST",
            headers:{'Content-type':'application/json',},
            url:URL+url,
            data:data,
            //withCredentials:true
        }).then(function(res){
            //alert('post:'+res)
            console.log(url+'	Post请求到:');
            console.log(res);
            //alert('post-response:'+res);
            callback(that,res);
            //ajax_get('/manage/getinfo',this);
        }).catch(function(error){
            alert('post失败')
            console.log(error);
        });
    }
    function ajax_get(url,that,callback){
        axios({
            method:"GET",
            headers:{'Content-type':'application/json',},
            url:URL+url,
            withCredentials:true
        }).then(function(res){
            console.log(url+'	Get请求到:')
            console.log(res);
            //alert('get:'+this.res);
            callback(that,res);
    
        }).catch(function(error){
            alert('get下载失败')
            console.log(error);
        });
    }
    function ajax_post_params(url,data,that,callback=()=>{}){
        axios({
            method: 'post',
            url: URL+url,
            headers: {
                'Content-type': 'application/x-www-form-urlencoded',
            },
            params:data,
        })
        .then(function(res){
            //alert('post:'+res)
            console.log(url+'	Post请求到:');
            console.log(res);
            //alert('post-response:'+res);
            callback(that,res);
            //ajax_get('/manage/getinfo',this);
        }).catch(function(error){
            alert('post失败')
            console.log(error);
        });
    }
    

      

  • 相关阅读:
    python下载.msg文件的附件
    python如何提取word内的图片
    python如何实现对word内段落文本及表格的读取
    python中使用to_excel时如何不覆盖原有数据来新建sheet页
    用python获取表格中的节假日起始日期
    如何利用python的xlrd模块读取日期格式的Excel
    HTML简介
    前端开发工程师
    测试工程师养成记
    电子沙盘
  • 原文地址:https://www.cnblogs.com/cina33blogs/p/9791556.html
Copyright © 2020-2023  润新知