Http
为了发布到我的API,当响应的状态为200时,它会正确地解析它并按预期输出我的响应,我使用第一个
.then()
.catch()
阻止。
我的问题是
.catch()
api中没有我的原始json错误,只是未定义。API的错误响应如下所示:
{
"status": 400,
"message": "Bad request",
"errors": [
{
"code": "IN_USE",
"message": "Email is in use",
"field": "email"
}
],
"data": []
}
我已经确定我要
application/json
头同时有200和400响应,所以我不确定为什么我的typescript代码不会解析json
.catch()
我的打字稿由两个文件组成
api
和
user
,用户只需扩展api以使用
post()
我创建的方法:
api.ts标准
public post(path, params): Promise<any> {
return this.http
.post(this.apiUrl + path, params)
.toPromise()
.then(r => r.json());
}
用户.ts
register(name: string, email: string, password: string) {
this.post('/register', {
access_token: this.accessToken,
name: name,
email: email,
password: password
})
.then(r => {
console.log(r);
})
.catch(r => {
console.log(r);
});
}