【发布时间】:2020-03-10 03:02:23
【问题描述】:
我有proxy.conf.json
{
"/api/*": {
"target": "https://api.dev.xyz.com",
"secure": false,
"changeOrigin": true,
"pathRewrite": { "^/api": "" },
"logLevel": "debug"
}
}
我在package.json 文件中有ng serve --proxy-config proxy.conf.json。当我使用npm start 运行我的项目时,它显示错误
ng serve --proxy-config proxy.conf.json
Browserslist: caniuse-lite is outdated. Please run next command `npm update`
10% building 3/3 modules 0 active[HPM] Proxy created: /api -> https://api.dev.xyz.com
[HPM] Proxy rewrite rule created: "^/api" ~> ""
[HPM] Subscribed to http-proxy events: [ 'error', 'close' ]
我已经检查了多个堆栈溢出答案reference link,但我仍然被困在这里很长时间了。
【问题讨论】:
-
您在哪里看到错误?除了 caniuse-lite 包,没有错误。它告诉您重写规则已被应用(这意味着当您发出像
http.get<any>('/api/something')这样的http 请求时,它会将/api/something替换为https://api.dev.xyz.com/something。 -
错误到底在哪里?
-
我有像
https://api.dev.xyz.com/api/login这样的 API 调用,当我尝试点击静止时它显示blocked by cors error in the browser console。 -
如何创建请求?是
this.http.get('https://api.dev.xyz.com/api/login')还是this.http.get('/api/login')(甚至this.http.get('/api/api/login'),给定代理文件中的路径重写)? -
我正在以
this.http.get('https://api.dev.xyz.com/api/login')的身份创建请求
标签: angular proxy angular-cli