superman66 / vue2.x-douban

Vue2.0实现简易豆瓣电影webApp
http://superman66.github.io/vue2.x-douban/dist/#/
799 stars 292 forks source link

使用proxyTable设置代理无法访问 #11

Closed Layol closed 7 years ago

Layol commented 7 years ago

我仿造这个写了一遍,直接访问api跨域,想用proxyTable设置代理可是报了404,换成您这种node代理就成功了,我想问一下是跟头部信息处理之类的有关还是?

superman66 commented 7 years ago

应该没有关系。proxyTable 的方式我之前也用过,是正常的。 关于 proxyTable 的设置你可以参考下这个版本的代码:index.js

Layol commented 7 years ago
proxyTable: {
    '/api': {
    target: 'http://api.douban.com/v2',
    changeOrigin: true,
    pathRewrite: {
      '^/api': ''
    }
  }

}, 这个是我的配置 const HOST = '/api' export function fetch (url) { return new Promise((resolve, reject) => { axios.get(HOST + url) .then(response => { resolve(response.data) }).catch(err => { console.log(err) }) }) } 这个连接api的 GET http://localhost:8080/api/movie/coming_soon?start=0&count=9 404 (Not Found) Error: Request failed with status code 404 at createError (createError.js?f777:16) at settle (settle.js?199f:18) at XMLHttpRequest.handleLoad (xhr.js?14ed:77) 报的错是这个,请问您能不能看出啥明显的错误或者问题

superman66 commented 7 years ago

根据你提供的代码来看,应该是正确的。你把 proxyTable 所在的配置文件完整贴出来看看

Layol commented 7 years ago

// see http://vuejs-templates.github.io/webpack for documentation. var path = require('path')

module.exports = { build: { env: require('./prod.env'), index: path.resolve(dirname, '../dist/index.html'), assetsRoot: path.resolve(dirname, '../dist'), assetsSubDirectory: 'static', assetsPublicPath: '/', productionSourceMap: true, // Gzip off by default as many popular static hosts such as // Surge or Netlify already gzip all static assets for you. // Before setting to true, make sure to: // npm install --save-dev compression-webpack-plugin productionGzip: false, productionGzipExtensions: ['js', 'css'], // Run the build command with an extra argument to // View the bundle analyzer report after build finishes: // npm run build --report // Set to true or false to always turn it on or off bundleAnalyzerReport: process.env.npm_config_report }, dev: { env: require('./dev.env'), port: 8080, autoOpenBrowser: true, assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: { '/api': { target: 'http://api.douban.com/v2', changeOrigin: true, pathRewrite: { '^/api': '' } } }, // CSS Sourcemaps off by default because relative paths are "buggy" // with this option, according to the CSS-Loader README // (https://github.com/webpack/css-loader#sourcemaps) // In our experience, they generally work as expected, // just be aware of this issue when enabling this option. cssSourceMap: false } }

Layol commented 7 years ago

我重启了一下项目,发现可以了,不好意思耽误你时间了,谢谢!