vue 本身是不支持 ajax 接口请求的,所以我们需要安装一个接口请求的 npm 包,来使我们的项目拥有这个功能。支持 ajax 的工具有很多。一开始,我使用的是 vue-resource这个工具。但是我后来发现其开发人员在一年就停止维护了,所以后来便改为了axios。
以下为 Axios api 接口文件的配置过程:
一、安装 axios 工具(在项目文件目录下)
npm install axios -D
二、新建了一个 src/api/index.js 这个空文本文件,并为其添加如下代码:
import axios from 'axios';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
import qs from 'qs';
let base = 'http://xxxx.xxxx.com.cn/hubei/doc';
let base1 = 'http://xxxx.com';
let token = '';
let http = axios.create({
baseURL: 'http://localhost:8080/',
withCredentials: true,
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
},
transformRequest: [function (data) {
let newData = '';
for (let k in data) {
if (data.hasOwnProperty(k) === true) {
newData += encodeURIComponent(k) + '=' + encodeURIComponent