簡體   English   中英

如何使用HttpClient在angular 5中為Web請求設置超時

[英]how can I set a timeout for the web request in angular 5 using HttpClient

通過定義超時,您可以設置時間限制以等待Web請求。 超時,是Web請求無法通過的時間限制。 例如,如果我將超時時間定義為3秒,則在請求數據時超過3秒將取消Web請求。 我希望我的網絡服務不超過3秒。 我該怎么做? 我是angular.js領域的新手。

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請求的屬性“超時”。 沒有設置超時

您可以使用以下超時操作符:

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')
         );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM