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

axios cross-domain problem

Problem Description:

  • After using vue+axios to develop a post request, the cross-domain problem in the following picture appears;

Insert picture description here

Solution:

I read some related information on the Internet and finally found a solution

module.exports = {
    
    
    dev: {
    
    
        //...
        proxyTable: {
    
    
      '/api': {
    
    
        port: 3000,
        target: 'http://127.0.0.1:8081/',
        chunkOrigins: true,
        pathRewrite: {
    
    
          '^api': ''
        }
      }
    }
}
  • Modify proxyTable in the index.js file in the config directory
  • chunkOrigins is whether to enable cross-domain
axios.request({
    
    
          url: '/api/sysUser/selectUserInfo',
          method: 'POST'
        }).then(res => {
    
    
          console.log(res);
        }).catch(res => {
    
    
          console.log('error' + res);
        })

Modify axios in the vue component method

Restart the project and send the post request again, the program is normal
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40240053/article/details/108763984