我正在向具有以下路由的服务器发出请求:
app.use('/401', (req, res) => res.status(401).end());
app.use('/403', (req, res) => res.status(403).end());
app.use('/404', (req, res) => res.status(404).end());
app.use('/500', (req, res) => res.status(500).end());
app.use('/502', (req, res) => res.status(502).end());
app.use('/503', (req, res) => res.status(503).end());
app.use('/504', (req, res) => res.status(504).end());
当我用 Angular 提出请求时(/404
, {}
):
public async post(path: string, data: object): Promise<Response> {
try {
return await this.http.post(path, data).toPromise();
} catch (err) {
console.log('err', err);
throw err;
}
}
I get:
ok: false
status: 0
statusText: ""
type: 3
在 Chrome 控制台中,我看到请求是通过OPTIONS
它确实返回了 404:
Request URL: http://localhost:3000/404/
Request Method: OPTIONS
Status Code: 404 Not Found
它去哪儿了?如何获取真实的错误代码?
我读到这可能是 CORS 问题...我的应用程序在 4200 上,我的服务在 3000 上。在我的服务中,我在顶部(在其他任何事情之前):
app.use(function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Methods", "*");
res.setHeader("Access-Control-Allow-Headers", "*");
next();
});
我不认为这是 COR 的问题...
但我不知道,可能是这样吗?
我不应该得到err
有地位404
?