zoukankan      html  css  js  c++  java
  • 请求接口的封装

    1. get方法
      1. /*方法说明
          *@method get
          *@param { path:接口路径, 
                    arg:参数,
                    callback: 成功回调的函数, 
                    err:失败回调的函数
                  }
          *@return {
                    callback():成功函数 ,
                    err(): 失败的函数
                   } 
          */
          get(path: any, arg: object, callback: Function, err: Function) {
            axios.get(url.return.host + path,  {
                headers: {
                "Authorization":localStorage.getItem("token"), 
                }, 
                params:arg     
              })
              .then(function (response) {
                  return callback(response); 
              })
              .catch(function (error) {
                // console.log(error.status_code)
                  return err(error);
             }); 
          },
    2. post方法
      1. /*方法说明
          *@method post
          *@param { path:接口路径, 
                    arg:参数,
                    head: 请求头
                    callback: 成功回调的函数, 
                    err:失败回调的函数
                  }
          *@return {
                    callback():成功函数 ,
                    err(): 失败的函数
                   } 
          */
          post(path: any, head: any, arg: any, callback: Function, err: Function){
            axios({
              method: "post",
              url: url.return.host + path,
              headers: head,
              data: arg,
            })
            .then(function (response) {
              return callback(response);
            })
            .catch(function (error) {
              return err(error);
            }); 
          },
    3. patch方法    
    4. /*方法说明
        *@method pat
        *@param { path:接口路径, 
                  head: 请求头
                  arg:参数,
                  callback: 成功回调的函数, 
                  err:失败回调的函数
                }
        *@return {
                  callback():成功函数 ,
                  err(): 失败的函数
                 } 
        */
        patch(path: any, head: any, arg: any, callback: Function, err: Function){
          axios({
            method: "patch",
            url: url.return.host + path,
            headers: head,
            data: arg,
          })
          .then(function (response) {
            return callback(response);
          })
          .catch(function (error) {
            return err(error);
          }); 
        }



  • 相关阅读:
    KindEditor-编辑器配置参数属性
    泛型作为返回类型的写法
    ObservableCollection<T> 类
    常遇到的问题
    实现跨浏览器html5表单验证
    mysql 密码重置
    Web用户的身份验证及WebApi权限验证流程的设计和实现
    Discuz3.2 新用户插入数据库SQL
    3. 深入研究 UCenter API 之 加密与解密(转载)
    window.open实现模式窗口
  • 原文地址:https://www.cnblogs.com/zwh520/p/10272350.html
Copyright © 2011-2022 走看看