向 API 发送 GET 请求时,我总是收到错误(因此响应将落入 CATCH 而不是 TRY),即使强硬状态为 200。
这是请求:
// Check if user exists at blur on email input authentication
checkUserExists( platformId: string, email: string) {
return this.http.get(checkUserExistsAPI + `/${platformId}/${email}`);
}
Run Code Online (Sandbox Code Playgroud)
当我使用 Angular 5 时,我去掉了 .map(res)。
这是使用请求的函数:
// Verify if emails exists
verifyEmail(email) {
if (email) {
this.authenticationService.checkUserExists( this.platformId, email )
.subscribe(
(data: CheckEmailResponse) => {
console.log(data);
},
(error: CheckEmailResponse) => {
console.log(error);
}
);
}
}
Run Code Online (Sandbox Code Playgroud)
它永远不会 console.log(data) 因为我总是收到这个错误:
error:{
error: SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at XMLHttpRequest.onLo…, text: ""}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}
message:"Http failure during parsing for http://API.com"
name:"HttpErrorResponse"
ok:false
status:200
statusText:"OK"
Run Code Online (Sandbox Code Playgroud)
好吧,我想通了。响应没有任何内容主体,并且 Angular 5 默认情况下需要 Json 格式的内容主体,因此它无法工作。无论如何,我只需要状态响应就可以知道它是 200 还是 404。
我{ responseType: 'text' }按照上面的方式添加到请求中:
return this.http.get(checkUserExistsAPI +/${platformId}/${电子邮件}, { responseType: 'text' });
所以现在它设法转到我的 TRY/CATCH 的 TRY,数据等于“null”。但我不关心数据,因为我只关心状态。不管怎样,现在如果状态是200,就会进入TRY,如果状态是404,就会进入CATCH。
| 归档时间: |
|
| 查看次数: |
10161 次 |
| 最近记录: |