Vue2.x 版本解決跨域問題
阿新 • • 發佈:2020-12-22
通過proxyTable將介面代理到本地
在開發的時候,需要請求同區域網內的介面,發現直接使用http://對方的ip地址/介面路徑,會出現類似下圖的跨域報錯
開啟config/index.js,在dev中找到proxyTable
proxyTable: {
// 下面是介面的路徑,我的介面 地址是http://ad.why.com/login
'/api': {
// 需要轉發代理的域名
target: 'http://ad.why.com',
// 如果是https介面,需要配置這個引數
secure: false,
// 如果介面跨域,需要進行這個引數配置
changeOrigin: true,
// 這是一個萬用字元,設定完了之後每個介面都要在前面加上/api(特別注意這一點)
pathRewrite: {
'^/api': '/'
}
}
},
/**
* @description: 登入部分api配置
*/
import { get, post } from './http';
export const apiLogin = (p) => post('/api/base/login', p);
export const apiGetUserName = (p) => get('/api/user-me', p);
export const apiGetMenuList = (p) => post('/api/menus/me', p);
export const apiGetUserType = (p) => get('/api/users/current', p);