VueJS前端和SailsJS后端之间的Axios CORS问题

问题描述

我已经把厨房水槽丢了,但是仍然无法从VueJs前端到SailsJS后端建立Axios连接。 Vue在localhost:8080上运行,而Sails在localhost:1337上运行。这是错误消息和一些相关设置-

enter image description here

Vue-vue.config.js

module.exports = {
  devServer: {
    proxy: {
      '/': {
        target: 'http://localhost:1337',changeOrigin: true,pathRewrite: {
          '^/': ''
        }
      }
    }
  }
}

Vue-src / views / Example.vue

axios.defaults.baseURL = 'http://localhost:8080'
  axios.defaults.crossDomain = true
  axios.defaults.headers['Access-Control-Allow-Origin'] = '*'


  const response = await axios.get('http://localhost:1337/hello')

风帆-config / security.js

module.exports.security = {

  cors: {
    allRoutes: true,allowOrigins: '*',allowCredentials: false,methods: 'GET,POST,PUT,DELETE,OPTIONS,HEAD',headers: 'content-type',allowAnyOriginWithCredentialsUnsafe: true,}

};

风帆-api / controllers / ExampleController.js

module.exports = {
  hello: function(req,res) {
    return res.status(200).send('hello');
  }
}

基于其他SO建议,我对这些设置进行了很多修改,但似乎没有任何办法可以解决此CORS错误。有什么想法吗?

编辑

我找到了一个可行的解决方案,尽管并不理想。如果我在操作级别添加此内容,它将发送一个我的前端将接受的响应。

风帆-api / controllers / ExampleController.js

module.exports = {
  hello: function(req,res) {
    res.setHeader('Access-Control-Allow-Origin','http://localhost:8080');

    return res.status(200).send('hello');
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)