1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Vue项目设置代理解决开发环境下的跨域问题

Vue项目设置代理解决开发环境下的跨域问题

时间:2024-03-29 17:41:12

相关推荐

Vue项目设置代理解决开发环境下的跨域问题

首先需要安装axios,推荐使用npm安装

$ npm install axios --save

配置config->index.js中的proxyTable,内容如下:

proxyTable: {'/api': {target: '',// 请换成你的地址changeOrigin: true,pathRewrite: {'^/api': ''}}},

以下为get请求例子

在需要使用axios的vue中引入axios,如下:

baseURL中的api即为index.js中的target值,因此,axios请求的地址实际是/product/list.do?keyword=手机

运行结果为:

post请求举例:

axios.post({method: 'post',baseURL: 'api',url: 'test',data: {firstName: 'Fred',lastName: 'Flintstone'}}).then(function (response) {console.log(response);}).catch(function (error) {console.log(error);});

执行多个并发请求:

function getUserAccount() {return axios.get('/user/12345');}function getUserPermissions() {return axios.get('/user/12345/permissions');}axios.all([getUserAccount(), getUserPermissions()]).then(axios.spread(function (acct, perms) {// 两个请求现在都执行完成}));

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。