每天一个报错,个各不相同。
看见下面的这行报错是不是感觉跟熟悉?
没错,vue的跨域问题,记录一下我的解决方法并分享给大家 ~ . ~
Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
那就直接上代码?
module.exports = {
devServer: {
open: true, //自动打开浏览器
host: 'localhost',
port: 8080,
https: false,
//以上的ip和端口是我们本机的;下面为需要跨域的
proxy: {
//配置跨域
'/api': {
target: 'http://11111111111.com',//填写你们真实的后台接口
ws: true,
changOrigin: true,//允许跨域
pathRewrite: {
'^/api': ''//请求的时候使用这个api就可以
}
}
}
}
}
我的vue文件中没有config文件夹,所以新建了个vue.config.js文件,放在根路径就好。
需要跨域的页面
mounted(){
this.job()
},
methods:{
job(){
post("/api/index.php/user/index/postList",{
//这个里面的写路径的后面一部分,前面记得加上/api
token:"",
page: this.page,
count: this.count,
type: