Angular CLI Proxy conf is not working for POST request
我在使用 angular 5 的 proxy.conf 时遇到了一个问题。我试图从 localhost 上运行的 angular 应用程序访问 https 开发服务器(证书无效)。我拥有的代理非常适合 GET 请求。但是对于 POST 请求,它失败并出现以下错误
OPTIONS https://www.dev.xxxx.au/api/carg/v1/enquiry 405 ()
Failed to load https://www.dev.xxxx.au/api/carg/v1/enquiry: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4455' is therefore not allowed access. The response had HTTP status code 405.
我知道 OPTIONS 在 POST 之前由浏览器调用,但找不到使其工作的方法。
以下是我的代理配置
1 2 3 4 5 6 7 8 9 10 11 | { "/api": { "target":"https://www.dev.xxxxx.au", "secure": false, "changeOrigin": true, "logLevel":"debug", "pathRewrite": { "^/api/carg/v1/enquiry":"https://www.dev.xxxx.au/api/enquiry" } } } |
尝试了很多组合,但都无法奏效。有什么想法...?
正如@LucasTétreault 指出的那样,问题在于 HttpClient 指向的是绝对 url(我的错)。
1 2 3 4 | this.url = environment.CHS_BASE_URL; let path = '/api/carg/v1/enquiry'; this.url = this.url + path; return this.httpClient.post<EnquiryResponse>(this.url,request, config); |
如下更正,它工作顺利
1 2 3 4 | //this.url = environment.CHS_BASE_URL; let path = '/api/carg/v1/enquiry'; //this.url = this.url + path; return this.httpClient.post<EnquiryResponse>(path,request, config); |