http post请求中的Angular2超时 [英] Angular2 timeout in http post request

查看:595
本文介绍了http post请求中的Angular2超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在发布请求中超时3秒?怎么样?

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

我目前的代码

this.http.post('myUrl', 
    MyData, {headers: Myheaders})
        .map(res => res.json())
        .subscribe(
            data => this.ret = data,
            error => console.debug('ERROR', error),
            () => console.log('END')
        );


推荐答案

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

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

这篇关于http post请求中的Angular2超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆