【问题标题】:Http Get catch is not working in angular 2Http Get catch 在角度 2 中不起作用
【发布时间】:2017-08-17 12:02:36
【问题描述】:

我有一个带有以下内容的 service.ts 类:

getData(username:string, password:string) {

    let params: URLSearchParams = new URLSearchParams();
    params.set('username', username);
    params.set('password', password);


    let requestOptions = new RequestOptions();
    requestOptions.search = params;
    return  this._http.get(this._url,requestOptions).map((res:Response)=>res.status)
   .catch((error: any) => {
                if (error.status === 500) {
                    return Observable.throw(new Error(error.status));
                }
                else if (error.status === 400) {
                    return Observable.throw(new Error(error.status));
                }
                else if (error.status === 409) {
                    return Observable.throw(new Error(error.status));
                }
                else if (error.status === 406) {
                    return Observable.throw(new Error(error.status));
                }
            });
}

我的 component.ts 类是:

    onCLick(myForm : NgForm) {
    this._service.getData(this.userName, this.password).subscribe(resData => this.formStatus = resData,
    resError => this.formStatus = resError); 
    if (this.formStatus === 400) {
    this.router.navigateByUrl('/Home');
    } else if (this.formStatus === 200) {
    console.log(this.formStatus );
    } else {
       console.log(this.formStatus );
    }
  }

在运行页面时,即使响应是 status = 400 的错误,但在调试时它最初并没有进入 service.tscatch 块(服务器命中后),而是移动到 component.ts,在函数 *onClick()* 中,然后在执行后进入 catch 块,这对我来说看起来很奇怪。我希望执行 catch 块,而不是执行 return 语句,因为存在错误。 P.S: formStatus 在从表单中引用双向绑定。

因此

if (this.formStatus === 400) {
    this.router.navigateByUrl('/Home');
    } else if (this.formStatus === 200) {
    console.log(this.formStatus );
    } else {
       console.log(this.formStatus );
    }

由于未设置状态,上述部分代码不起作用。

【问题讨论】:

    标签: angular angular2-services


    【解决方案1】:

    你是怎么调试的?放置 console.log 来捕获块。

    我认为在异步执行后立即检查条件不是一个好习惯。

    这部分执行异步:

    this._service.getData(this.userName, this.password).subscribe(resData => this.formStatus = resData,
        resError => this.formStatus = resError); 
    

    这部分可能无法正确执行,因为this.formStatus可能还没有初始化。

    if (this.formStatus === 400) {
    this.router.navigateByUrl('/Home');
    } else if (this.formStatus === 200) {
    console.log(this.formStatus );
    } else {
       console.log(this.formStatus );
    }
    

    你必须在错误块中检查它。

    【讨论】:

    • 您是对的,但请务必为我澄清以下内容: 1. 异步是什么意思? 2.能否请您在代码中纠正我,我尝试了很多方法,但找不到通过的方法。如果我在错误块中添加,我可能无法处理成功场景,对吗?我是 Angular 2 的新手,所以有这些疑问。
    • 当您执行 .subscribe(订阅 observable)时,它会异步执行,并且当下一个值发出 onSuccess 或 onError 时。该代码不会阻塞线程执行,这就是为什么 .subscribe 的下一个代码会立即执行。您需要处理 onError 块 400、500 中的 http 错误,以及 onSuccess 块中的成功。 this._service.getData(this.userName, this.password).subscribe(resData => this.formStatus = resData, // this is success block resError => { this.formStatus = resError; //check error status here }); // this is error block
    • 非常感谢您的回复,但是onClick(loginForm : NgForm) { this._service.getData(this.userName, this.password).subscribe( resData => this.formStatus = resData, resError => { this.formStatus = resError if (this.formStatus === 400) { this.router.navigateByUrl('/Home'); } });我对此进行了相应的更改,但是当调试点在错误块内时,this.formStatus仍未初始化,其值未定义。请看一下
    • 我认为问题出在return Observable.throw(new Error(error.status)); 这是您的自定义对象吗? Error 还是标准 es6 错误?
    • 我更正了它并且它现在可以工作了。非常感谢:)
    猜你喜欢
    • 2017-11-11
    • 2023-03-26
    • 2017-02-23
    • 2019-01-17
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 2016-10-01
    相关资源
    最近更新 更多