鍍金池/ 問(wèn)答/數(shù)據(jù)庫(kù)  HTML/ vue前后端端口號(hào)不同,proxytable代理跨域無(wú)效

vue前后端端口號(hào)不同,proxytable代理跨域無(wú)效

前后端都部署在我的電腦上,只有端口號(hào)不同,前端用的vuecli,采用自帶的proxytable設(shè)置跨域,但是ajax發(fā)送請(qǐng)求的時(shí)候,卻沒(méi)有變端口號(hào),狀態(tài)返回404

dev配置如下

 dev: {
    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
         // 去掉這個(gè)注釋
      'admin': {
        target: 'http://localhost:8004',
    

        changeOrigin: true,
        pathRewrite: {
          '^/admin': ''
        }
      }
    },

    // Various Dev Server settings
    host: 'localhost', // can be overwritten by process.env.HOST
    port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    autoOpenBrowser: false,
    errorOverlay: true,
    notifyOnErrors: true,
    poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-

    
    /**
     * Source Maps
     */

    // https://webpack.js.org/configuration/devtool/#development
    devtool: 'cheap-module-eval-source-map',

    // If you have problems debugging vue-files in devtools,
    // set this to false - it *may* help
    // https://vue-loader.vuejs.org/en/options.html#cachebusting
    cacheBusting: true,

    cssSourceMap: true
  },

控制臺(tái)狀態(tài)如下:

Request URL: http://localhost:8081/admin/register1?0=n&1=a&2=m&3=e&4=%3D&5=%22&6=l&7=o&8=u&9=t&10=o&11=n&12=g&13=%22&14=%26&15=p&16=w&17=d&18=%3D&19=%22&20=1&21=1&22=1&23=1&24=%22
Request Method: GET
Status Code: 404 Not Found
Remote Address: 127.0.0.1:8888
Referrer Policy: no-referrer-when-downgrade

求問(wèn)大家如何解決?

回答
編輯回答
念初

proxyTable改動(dòng)一下:

 dev: {
    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
         // 去掉這個(gè)注釋
      '/admin': {
        target: 'http://localhost:8004',
        changeOrigin: true
      }
    },

    // Various Dev Server settings
    host: 'localhost', // can be overwritten by process.env.HOST
    port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    autoOpenBrowser: false,
    errorOverlay: true,
    notifyOnErrors: true,
    poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-

    
    /**
     * Source Maps
     */

    // https://webpack.js.org/configuration/devtool/#development
    devtool: 'cheap-module-eval-source-map',

    // If you have problems debugging vue-files in devtools,
    // set this to false - it *may* help
    // https://vue-loader.vuejs.org/en/options.html#cachebusting
    cacheBusting: true,

    cssSourceMap: true
  },
2017年12月22日 17:05
編輯回答
喵小咪

您好,您這個(gè)問(wèn)題解決了嗎?我也遇到這個(gè)問(wèn)題了。

2017年9月19日 12:12
編輯回答
墨染殤

貼一下代理

proxyTable = {
  '/api': {
    target: 'http://localhost:8004/',
    changeOrigin: true,
    pathRewrite: {
      '^/api': '/'
    }
  }
}

比如后端接口為http://localhost:8004/query,那么在代碼中的請(qǐng)求url可以寫成/api/query。
另外代理proxyTable并不是直接請(qǐng)求你的后端接口,而是在cli層面啟動(dòng)了一個(gè)server,你的代理請(qǐng)求先會(huì)到這個(gè)server,然后server再去你的接口進(jìn)行請(qǐng)求,所以你看到的ajax請(qǐng)求沒(méi)有改變url。

2017年3月13日 16:49