我总是遇到错误
ERROR错误:未捕获(按承诺):e:{“ headers”:{“ normalize dNames”:{},“ lazyUpdate”:null},“ status”:400,“ statusText”:“ Bad Request”
代码为:(已更新)
async verify() {
let schema = {"token": localStorage.getItem('id_token')};
let response: any = await this.http.post('/api-token-verify/', schema).toPromise();
return response;
}
public async tokenAliveState() {
console.log("1");
let stuff = await this.verify();
console.log("2");
console.log("Returning from tokenAliveState");
return tokenNotExpired('id_token');
}
我要做的就是发布信息并等待响应,然后再继续。响应是纯JSON。
角核4。
有什么想法吗?需要更多信息吗?
在继续执行之前,有没有人有一个简单函数的工作示例等待http响应?我在网上找到的所有示例都显示与此类似,但无论我做什么,都会遇到未捕获的异常。
答案 0 :(得分:0)
“东西”是一个承诺。您需要.then()和.catch()异步调用的响应/错误。基本上,您的http呼叫没有通过,您也没有捕获到Promise返回的错误。
stuff
.then(() => {})
.catch((error) => {})
应该使您的错误消失。
答案 1 :(得分:0)
您应该始终将try调用包装在try catch块中,然后进行相应处理。
try {
let response: any = await this.http.post('/api-token-verify/', schema).toPromise();
} catch (err) {
// Handle the 400 http code here.
}