这是我的vue跨域配置
proxyTable: {
'/api':{
target:'https://api.douban.com',
changeOrigin:true,
pathRewrite:{
'^/api':''
},
'/node':{
target:'http://127.0.0.1:3000',
changeOrigin:true,
pathRewrite:{
'^/node':''
}
}
}
以上配置第一个跨域请求豆瓣音乐好用,第二个跨域本地node服务器不好用,该服务器是网上大神写的一个网易云音乐的api
我的axios请求
getMusician: function({commit}){
axios.get('/node/personalized/newson')
.then(function(response){
console.log(response);
commit('addMusician', response.result.slice(0,4));
})
.catch(function(err){
console.log(err)
})
}
node服务器的跨域配置
app.all('*', function(req, res, next) {
if (req.path !== '/' && !req.path.includes('.')) {
res.header('Access-Control-Allow-Credentials', true)
// 这里获取 origin 请求头 而不是用 *
res.header('Access-Control-Allow-Origin', req.headers['origin'] || '*')
res.header('Access-Control-Allow-Headers', 'X-Requested-With')
res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
res.header('Content-Type', 'application/json;charset=utf-8')
}
next()
})
这个我跨域请求时浏览器报错
哪位大神能帮帮忙
因为返回的错误是404。应该是你本地的node服务器跑起来的路径和你请求的路径不一致, 你仔细检查一下.