zoukankan      html  css  js  c++  java
  • axios

    Axios 是由原生的XMLRequest和Priomse相结合的http库,进行对数据的请求等操作 。

    安装:

     npm install axios

     使用:

    Get:

    axios.get('/user', {
        params: { //连接参数
          ID: 12345
        }
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });

    Post:

    axios.post('/user', { //提交的数据
        firstName: 'Fred', 
        lastName: 'Flintstone'
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });

    多个请求并发的时候:

    axios.all([getUserAccount(), getUserPermissions()]) //和Promise基本相同
      .then(axios.spread(function (acct, perms) {
        // 两个请求现在都执行完成
      }));

    axios的config参数:

      

    const config = {
        url:"/user", //数据地址
        method:"get", //请求方式 
        baseURL : "http://smome-domain.com/api/",//将baseURL自动添加到url之前,兴程绝对的URL
        transformRequest:[
            //允许在向服务器发送前修改数据,只能用在PUT,POST ,PATCH这几个请求方法中
            //后面数组中的函数必须返回一个字符串或是数据流
            function(data){
                return data;
            }
        ],
        transformResponse:[
            //在传递给then或是catch之前,允许对响应数据进行修改
            function(data){
                //对数据进行转化
                return data;
            }
        ],
        //自定义请求头
        headers:{"X-Requested-With": "XMLHttpRequest"},
        //请求发送的数据 必须是一个无格式对象
        params:{ 
            ID:1235
        },
        //负责 params序列化的函数
        paramsSerializer:function(params){
           // ....
        },
        //作为请求主体被发送的数据 只适用于PUT POST  PATCH
        data:{
            fistrName:"zzz"
        },
        //超时时间
        timeout:1000,
        //表示跨域请求时是否需要使用凭证
        withCredentials:false ,//默认的
        //允许自定义处理请求,以使测试更轻松 返回以恶搞有效的响应
        adapter:function(config){
            //...
        },
        //http的基础验证
        auth:{
            usename:'...',
            password:'....'
        },
        //服务器响应的数据类型
        responseType:"json",
        //xsrf token 的值的HTTP头的名称
        xsfHeaderName:'X_XSRF-TOKEN',
        //作用 xsrf token的值的cookie名称
        xsrfCookieName:'XSRF_TOKEN',
        //允许上传处理进度事件
        onUploadProgress:function(progressEvent){},
        //允许为下载处理进度事件
        onDownloadPeogress:function(){},
        maxContentLength:2000, //允许响应头内容的最大尺寸
        validStatus:function(){} ,//定义对于给定的HTTP的响应状态码是 resolve /reject
        //follow的最大重定数目
        maxRedirects:5,
       // `httpAgent` 和 `httpsAgent` 分别在 node.js 中用于定义在执行 http 和 https 时使用的自定义代理。允许像这样配置选项:
      // `keepAlive` 默认没有启用
      httpAgent: new http.Agent({ keepAlive: true }),
      httpsAgent: new https.Agent({ keepAlive: true }),
    
      // "proxy" 定义代理服务器的主机名称和端口
      // `auth` 表示 HTTP 基础验证应当用于连接代理,并提供凭据
      // 这将会设置一个 `Proxy-Authorization` 头,覆写掉已有的通过使用 `header` 设置的自定义 `Proxy-Authorization` 头。
      proxy: {
        host: "127.0.0.1",
        port: 9000,
        auth:  {
          username: "mikeymike",
          password: "rapunz3l"
        }
      },
    
      // `cancelToken` 指定用于取消请求的 cancel token
      // (查看后面的 Cancellation 这节了解更多)
      cancelToken: new CancelToken(function (cancel) {
      })
    
    }





  • 相关阅读:
    OpenCV 环境搭建( Win7 32位 / VS2010 / OpenCV2.4.8 )
    OpenCV 简介
    计算机视觉简介
    使用 sigaction 函数实现可靠信号
    可靠信号机制
    信号机制的两个思考
    信号的接收和处理
    【angular5项目积累总结】列表多选样式框(1)
    数组相关方法积累(vueag等特别常用)
    Angular 4+ 修仙之路
  • 原文地址:https://www.cnblogs.com/panjingshuang/p/12054483.html
Copyright © 2011-2022 走看看