gpt4 book ai didi

javascript - 如何捕获超时请求

转载 作者:行者123 更新时间:2023-11-30 08:22:25 25 4
gpt4 key购买 nike

这是我的代码:

properties.service.ts

get(stringParams) {
let headers = new Headers({
"X-AUTH-APIKEY": API_KEY
});

this.options = new RequestOptions({
headers: headers
});
return this.http
.get(`${API_URL}/properties/?${stringParams}`, this.options)
.timeout(50)
.map((response: Response) => response.json())
}



posts.ts

this.propertiesService.get(arrayParams).subscribe((data: any) => {

}), (err) => { console.log("timeoout")};

我在超时中设置了 50,所以被解雇了,但我无法以这种方式捕获错误

}), (err) => { console.log("timeoout")};

我收到这个错误:

TimeoutError {name: "TimeoutError", stack: "TimeoutError: Timeout has occurred↵    at new Time…http://localhost:8100/build/polyfills.js:3:10143)", message: "Timeout has occurred"

编辑:

this.propertiesService.get(arrayParams).subscribe((data: any) => {

}), (err) => { console.log("timeoout")};

get(stringParams) {

return this.http
.get(`${API_URL}/properties/?${stringParams}`, this.options)
.timeout(50)
.map((response: Response) => response.json())
.catch(this.handleError);
}

private handleError(error: any) {
return Observable.throw(error);
}

我尝试了这个解决方案,但我仍然收到此错误 enter image description here

解决方案:

代码应该是这样的(订阅里面的err):

}, (err) => { console.log("timeoout")});

最佳答案

err 应该在 subscribe

里面
this.propertiesService.get(arrayParams).subscribe((data: any) => {

}, (err) => { console.log("timeoout")});

您应该捕获异常:

get(stringParams) {

return this.http
.get(`${API_URL}/properties/?${stringParams}`, this.options)
.timeout(50)
.map((response: Response) => response.json())
.catch(this.handleError);
}

private handleError(error: any) {
return Observable.throw(error);
}

关于javascript - 如何捕获超时请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51267377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com