跨域问题解决:
测试 上边的代理 ,结果 报错如下 :
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin
'http://localhost:11000' is therefore not allowed access.
原因:浏览器的同源策略不允许跨域访问,所谓同源策略是指协议、域名、端口相同。
解决:采用代理的方式如proxyTable解决。
具体的配置如下:
1)修改api方法中url的定义,请求前加/api前缀
let apiUrl = sysConfig.xcApiUrlPre; //将请求前缀抽取为全局变量
export const page_get = id =>{
return http.requestQuickGet(apiUrl+'/cms/page/get/'+id)
}
2)在config/index.js下配置proxyTable
‘/api/cms’: {
target: ‘http://localhost:31001’, //以/api/cms开头的请求,代理请求http://localhost:31001
pathRewrite: {
‘^/api’: ‘’//实际请求去掉/api
}
3 )重新请求页面,会发现原先请求后端接口的 http://localhost:31001 地址变为请求 http://localhost:11000,说明http代理成功