1. 程式人生 > 其它 >Access to XMLHttpRequest at‘http://localhost:跨域問題解決

Access to XMLHttpRequest at‘http://localhost:跨域問題解決

技術標籤:vue-clivue-cli3

在vue.config.js裡配置跨域請求

跨域問題報錯

在這裡插入圖片描述

解決

  • 在vue.config.js中配置
module.exports = {
  
  // 配置跨域請求
  devServer: {
    port: 8080,
    host: 'localhost',
    open: true,
    https: false,
    proxy: {
      '/api': {
        target: 'http://需要請求的地址/',
        ws: true,
        changeOrigin: true,
        pathRewrite:
{ '^/api': '' } } } } }

在環境變數中或者在baseURL中更改地址值為’/api’

-.env.development

 VUE_APP_API_URL=  /api

-.env.production

VUE_APP_API_URL=  /api