Angular2 timeout in http post request

后端 未结 3 2032

Is it possible to make a timeout of 3 seconds in a post request ? How ?

My code for the moment

this.http.post(\'myUrl\', 
    MyData, {headers: Myhe         


        
3条回答
  •  悲&欢浪女
    2021-01-17 11:30

    You can use the timeout operator like this:

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

提交回复
热议问题