Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript http post订阅并尝试捕获_Javascript_Http_Typescript_Angular - Fatal编程技术网

Javascript http post订阅并尝试捕获

Javascript http post订阅并尝试捕获,javascript,http,typescript,angular,Javascript,Http,Typescript,Angular,我使用Angular2的http.post,有时头不会发送到CORS服务器。所以我想尝试,直到请求成功。但是这段代码挂在一个无休止的循环中 var headers = new Headers(); headers.append('Content-Type', 'text/plain'); this.again = true; while (this.again==true) { http.post('https://localhost:44300/a

我使用Angular2的
http.post
,有时头不会发送到CORS服务器。所以我想尝试,直到请求成功。但是这段代码挂在一个无休止的循环中

 var headers = new Headers();
 headers.append('Content-Type', 'text/plain');

 this.again = true;
        while (this.again==true) {
           http.post('https://localhost:44300/account/getip', "", { headers: headers })
                .subscribe(
                (res2) => {
                    try {
                        this.ip = res2.json();
                        this.ipstr = this.ip.replace(/\./g, '-');
                        this.again = false;
                     }
                    catch (err) {
                        console.error(err);
                    }
                }
                );
        }

如果要捕获请求中的错误,可以:

  • 使用
    subscribe
    方法的第二个回调:

    http.post('https://localhost:44300/account/getip', "", { headers: headers })
            .subscribe(
              (res2) => {
                (...)
              },
              (error) => {
                (...)
              }
            );
    }
    
  • 使用
    catch
    操作符:

    http.post('https://localhost:44300/account/getip', "", { headers: headers })
            .catch((error) => {
              (...)
            })
            .subscribe(
              (res2) => {
                (...)
              }
            );
    }
    
    http.post('https://localhost:44300/account/getip', "", { headers: headers })
    .retry(3)              
    .subscribe((res2) => {
      this.ip = res2.json();
      this.ipstr = this.ip.replace(/\./g, '-');
    })
    
关于重试请求,您可以通过这种方式利用
retry
操作符(使用
timeout
one):

您可以看到使用
delay
在执行新请求之前等待一段时间

您也可能会对本文感兴趣:


您可以使用
重试
操作符:

http.post('https://localhost:44300/account/getip', "", { headers: headers })
        .catch((error) => {
          (...)
        })
        .subscribe(
          (res2) => {
            (...)
          }
        );
}
http.post('https://localhost:44300/account/getip', "", { headers: headers })
.retry(3)              
.subscribe((res2) => {
  this.ip = res2.json();
  this.ipstr = this.ip.replace(/\./g, '-');
})

TypeError:http.post(…)。重试不是所有运算符的函数,您需要导入它
import'rxjs/add/operator/retry'这会使我的应用程序崩溃,请尝试他们的答案。可能导入错误?我必须转换到
我在使用函数时遇到问题。我试过:
import'rxjs/add/operator/timeout';导入“rxjs/add/operator/catch”
,但随后应用程序冻结了:-(你是否尝试过:
导入'rxjs/Rx';
。看看会发生什么…这也冻结了我的浏览器你能打开JavaScript控制台看看SystemJS加载了什么吗?我很笨。我的代码中仍然有
循环。现在它可以工作了。谢谢!