1

I am following the tutorial from Vue JS 2 Tutorial #32 - HTTP Requests with vue-resource to jsonplaceholder.typicode.com. If I don't proxy it, it'll give out CORS error.

vue.config.js:

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

HTTP post request:

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

Error:

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

I have tried:

Edits:

  • Changed '/api/post' to '/api/posts', still not working.

  • Tried changing '/api/posts' to 'https://jsonplaceholder.typicode.com/posts' which resulted in CORS error.

  • Added pathRewrite: { '^/api': '' } into vue.config.json proxy, still not working.

  • Tried Proxy changeOrigin setting doesn't seem to work, still not working.

1
  • i think the URL should be /api/posts Commented Aug 15, 2020 at 4:08

2 Answers 2

0

I see your request if going to your localhost instead of the actual server that you want to hit, have you tried changing this.$http.post('/api/post', { to this.$http.post('https://jsonplaceholder.typicode.com/api/post', {

For what I can see on vuecli's documentation the fact that you proxy the server doesn't mean that you need to stop writing the URL of that server on your requests.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I have. Which cause CORS Error.
0

Try restart your dev server. A full restart is required to apply the changes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.