首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用HttpClient为angular 5中的web请求设置超时

问如何使用HttpClient为angular 5中的web请求设置超时
EN

Stack Overflow用户
提问于 2018-05-11 13:43:45
回答 1查看 5K关注 0票数 0

通过定义超时,您可以设置等待web请求的时间限制。超时,是指web请求不能超过的时间限制。例如,如果我将超时定义为3秒,则在请求数据时,如果超时超过3秒,则取消web请求。我希望我的web服务不超过3秒。我该怎么做呢?我是angular.js领域的新手。

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs/Observable';
import { GLOBAL } from '../app.constantes';

@Injectable()
export class AppService{
public url: string;

constructor(
    public _http: HttpClient

){}
getAll(url,method,param): Observable<any>{
    let config={};
    config["timeout"]=3000;
    config["data"]=param ? param: {}; //in case of POST  this is the "data" property, with GET is "params" I believe..
    return this._http.post(url,config);

}

不,我不知道我的问题是否说得不清楚。但我指的是web请求的“超时”属性。未设置超时..

EN

回答 1

Stack Overflow用户

发布于 2018-05-11 13:50:14

您可以像这样使用超时运算符:

代码语言:javascript
复制
this.http.post('myUrl', 
        MyData, {headers: Myheaders})
         .timeout(3000, new Error('timeout exceeded'))
         .map(res => res.json())
         .subscribe(
           data => this.ret = data,
           error => console.debug('ERROR', error),
           () => console.log('END')
         );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50285716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档