码迷,mamicode.com
首页 > 移动开发 > 详细

axios 使用

时间:2017-12-11 16:08:20      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:ret   ror   nbsp   this   head   return   blog   turn   method   

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

 

axios 使用

标签:ret   ror   nbsp   this   head   return   blog   turn   method   

原文地址:http://www.cnblogs.com/qzccl/p/8023131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!