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请求

  • 相关阅读:
    Spring Boot学习笔记
    打造高效率的软件测试
    如何将测试结果在jenkins上发布
    如何在docker container中运行web自动化测试
    Jmeter中随机读取测试文件的内容
    如何提高UI自动化测试的质量
    mac系统上添加定时任务
    keypass口令管理实践
    GPG实践
    树的遍历
  • 原文地址:https://www.cnblogs.com/qzccl/p/8023131.html
Copyright © 2011-2022 走看看