vue main.js 设置

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
// import VueRouter from './router'
import VueRouter from 'vue-router'
import Axios from 'axios'  //引用
import QS from 'qs'
import Customers from './components/Customers'
import About from './components/About'
import Add from './components/Add'
import CustomerDetails from './components/CustomerDetails'
import Edit from './components/Edit'


Vue.config.productionTip = false  //阻止启动生产消息,常用作指令。

Vue.use(VueRouter)

Vue.prototype.$axios=Axios  //挂载到原形上
//axios全局配置
Axios.defaults.baseURL = 'http://localhost:3000';  // 此处设置全局适用  避免每个页面的接口请求都  用的接口 前缀
Axios.defaults.headers.post['Content-Type'] = 'application/json';
 //Axios.defaults.headers.post['Content-Type'] = 'x-www-from-urlencoded'; 


//拦截器


// 添加请求拦截器
Axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么

    if(config.method==="post"){  //只有在post请求是 时
        console.log(config.data);
        //config.data=QS.stringify(config.data) ; //如果 后台不接受原声json  格式参数 需要序列化参数,需要安装 QS插件;请求之前对请求参数进行格式转换  转化后  格式user_id=iwen%40qq.com&password=iwen123
        console.log(config.data);
    }

    //console.log(config);  //打印网络请求 
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });

// 添加响应拦截器
Axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
//console.log(response)
    if(!response.data){
        return{
            msg:"数据返回不合理"
        }
    }

    return response;
  }, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
  });

猜你喜欢

转载自www.cnblogs.com/jinsuo/p/11769123.html