Vue JS 2 자습서 # 32-HTTP 요청 의 자습서를 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]
나는 시도하였습니다 :
axios 및 vue-resource
편집 :
'/ api / post'
를'/ api / posts'
로 변경했지만 여전히 작동하지 않습니다.'/ api / posts'
를'https://jsonplaceholder.typicode.com/posts'
로 변경하려고 시도하여 CORS 오류가 발생했습니다.vue.config.json 프록시에
pathRewrite : { '^ / api': ''}
를 추가했지만 여전히 작동하지 않습니다.프록시 changeOrigin 설정이 작동하지 않는 것 같습니다 시도했지만 여전히 작동하지 않습니다.