Dam*_*ian 4 json http node.js angular
我已经将一个旧项目(Angular 2)重构为 Angular 6。除了 api 调用存在问题之外,一切运行良好。在登录组件上,当我提交表单时,会触发包含数据的 POST 请求,并且拦截器会添加某些标头(目前仅包含内容类型)。提交表单的代码:
this.authService.signIn(this.account)
.subscribe( res => {
console.log('RES -> ', res);
this.router.navigate([this.returnUrl]);
},
err => console.log(err));
Run Code Online (Sandbox Code Playgroud)
验证服务方法:
signIn(account: Account) {
const req = new HttpRequest(HttpMethods.Post, AuthService.signInUrl,{account: account});
return this.makeRequest(req);
}
private makeRequest(req: HttpRequest<any>): Observable<any> {
this.progressBarService.availableProgress(true);
return this.http.request(req)
.finally( () => this.progressBarService.availableProgress(false));
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,我console.log
添加的 被触发两次:第一次是{type: 0}
,第二次返回我需要的数据。我已经从拦截器中删除了所有内容,只留下了next.handle(req)
它,它也做了同样的事情。知道为什么我收到 2 个回复,第一个只是 吗{type: 0}
?
那是因为你使用this.http.request()
. 我猜第一个响应实际上是 OPTIONS 请求的响应。
如果您仍然坚持使用this.http.request()
,例如如果您使用它来上传文件,您可能需要使用 rxJstakeLast(1)
来获取您需要的响应。
这是参考。 https://angular.io/api/common/http/HttpClient#request
归档时间: |
|
查看次数: |
985 次 |
最近记录: |