繁体   English   中英

Vue vue.config.js 代理 404

[英]Vue vue.config.js proxy 404

我正在关注Vue JS 2 Tutorial #32 - HTTP Requests with vue-resource 到jsonplaceholder.typicode.com的教程。 如果我不代理它,它会给出 CORS 错误。

vue.config.js:

module.exports = {
  devServer: {
    proxy: {
      '^/api': {
        target: 'https://jsonplaceholder.typicode.com',
        ws: true,
        changeOrigin: true,
        pathRewrite: { '^/api': '' }
      }
    }
  }
}

HTTP 发布请求:

this.$http.post('/api/posts', {
    userId: 1,
    title: this.blog.title,
    body: this.blog.content,
}).then(function (data) {
  console.log(data)
});

错误:

XHR POST http://localhost:8080/api/posts [HTTP/1.1 404 Not Found 3ms]

我努力了:

编辑:

  • '/api/post'更改为'/api/posts' ,仍然无法正常工作。

  • 尝试将'/api/posts'更改为'https://jsonplaceholder.typicode.com/posts' ,导致 CORS 错误。

  • pathRewrite: { '^/api': '' }添加到 vue.config.json 代理中,仍然无法正常工作。

  • 尝试过Proxy changeOrigin 设置似乎不起作用,仍然不起作用。

如果转到您的本地主机而不是您要访问的实际服务器,我会看到您的请求,您是否尝试过将this.$http.post('/api/post', {更改为this.$http.post('https://jsonplaceholder.typicode.com/api/post', {

对于我在 vuecli 的文档中可以看到的事实,您代理服务器并不意味着您需要停止根据您的请求编写该服务器的 URL。

尝试重新启动您的开发服务器。 需要完全重新启动才能应用更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM