通过定义超时,您可以设置等待web请求的时间限制。超时,是指web请求不能超过的时间限制。例如,如果我将超时定义为3秒,则在请求数据时,如果超时超过3秒,则取消web请求。我希望我的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请求的“超时”属性。未设置超时..
发布于 2018-05-11 13:50:14
您可以像这样使用超时运算符:
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')
);https://stackoverflow.com/questions/50285716
复制相似问题