zoukankan      html  css  js  c++  java
  • axios 使用

    1、安装

      cnpm install axios --save

    2、引入

    import axios from 'axios'
    Vue.prototype.axios = axios

    3、使用

    this.axios.post(this.URL('mall/goods'), {
        access_token: ''
    }).then(function (response) {
        console.log(response);
    })

    4、配置拦截器

    // 请求拦截器
    axios.interceptors.request.use(function (config) {
        let _this = new Vue();
        var data = {
            platform:_this.getPlatform()
        };
        Object.assign(config.data, data)  
    // 在发送请求之前做些什么
      return config;
    }, function (error) {
        // 对请求错误做些什么
      return Promise.reject(error);
    });
    // 响应拦截器
    axios.interceptors.response.use(function (response) {
        // 对响应数据做点什么
        return response;
      }, function (error) {
        // 对响应错误做点什么
        return Promise.reject(error);
      });

    5、问题

      在发送请求的时候,会发送两条请求,第一条请求没有传参,第二条请求传参

      第一次发送的method是OPTIONS,进行预检,询问是否允许跨域,可以通过以下的方法去掉:

      在请求拦截器里面添加这句

    config.headers['Content-Type'] = 'application/x-www-form-urlencoded'

      来降级为简单请求,这样的话,就不会发送OPTIONS请求

  • 相关阅读:
    Unity中溶解shader的总结
    Unity Shader 知识点总结(二)
    Unity Shader 知识点总结(一)
    Unity优化之GC——合理优化Unity的GC
    nuxt中使用vant框架
    Redux第一节
    React动画库
    react一写工具
    几种下载第三方的方式有何不同
    React生命周期函数
  • 原文地址:https://www.cnblogs.com/qzccl/p/8023131.html
Copyright © 2011-2022 走看看