axios跨域问题 No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.

axios跨域问题

问题描述:

  • 在使用vue+axios开发进行post请求,然后出现以下图片中的跨域问题;

在这里插入图片描述

解决方法:

在网上看了一些相关资料,最后找到了解决方法

module.exports = {
    
    
    dev: {
    
    
        //...
        proxyTable: {
    
    
      '/api': {
    
    
        port: 3000,
        target: 'http://127.0.0.1:8081/',
        chunkOrigins: true,
        pathRewrite: {
    
    
          '^api': ''
        }
      }
    }
}
  • 修改config目录下的index.js文件中的proxyTable
  • chunkOrigins为是否开启跨域
axios.request({
    
    
          url: '/api/sysUser/selectUserInfo',
          method: 'POST'
        }).then(res => {
    
    
          console.log(res);
        }).catch(res => {
    
    
          console.log('error' + res);
        })

在vue组件方法中修改axios

重新启动项目再次发送post请求,程序正常
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_40240053/article/details/108763984