vue跨域本地node服务器

这是我的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()
})

这个我跨域请求时浏览器报错
clipboard.png
哪位大神能帮帮忙

阅读 2.8k
2 个回答

因为返回的错误是404。应该是你本地的node服务器跑起来的路径和你请求的路径不一致, 你仔细检查一下.

是解决跨域吗? header头部是可以吗? 或者是可以试试 cors 这个是个第三方包,中间件完成跨域

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题