问题描述
使用Vue.js框架开发时,需要调用到后端接口,由于前端程序需要占用一个端口,后端程序需要占用一个端口,跨域问题必然存在,所以在前端调用后端API时便会提示No ‘Access-Control-Allow-Origin’ headeris present on the requested resource. Origin ‘后端地址’ is therefore not allowed access.
解决方法
1、打开项目目录中config文件夹下的index.js文件
2、在dev集合中找到proxyTable项
3、将其改为:
proxyTable: {
'/api': {
target: 'http://localhost:8081/class',//后端地址
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
},
},
假设我请求的API是/api/login?action=login
那么,这段代码将会把上述请求地址中的关键字’/api’直接替换为target中的后端地址
即实际请求的为:http://localhost:8081/class/login?action=login
添加后,重启项目
此时,跨域问题就解决了