【问题标题】:Angular 5 HTTP post internal server error not able to subscribe in componentAngular 5 HTTP post 内部服务器错误无法在组件中订阅
【发布时间】:2021-06-08 13:57:44
【问题描述】:

我正在使用 Angular HTTP.post 方法调用此 API http://10.108.11.239:8080/project/setRule

  1. 我在浏览器网络选项卡中收到响应,但它在浏览器控制台选项卡中显示内部服务器错误。

服务方式

public postSetup(data: Object, options?: RequestOptionsArgs) {

    const headers = new Headers();
    headers.append('Content-Type', 'application/json');

    headers.append('USER', this.elem);
    let option = options ? options : new RequestOptions({ headers: headers });
    return this.http.post(this.API_URL, data, option)
        .map((response: Response) => response.json())
        .catch((error: any) => {
           console.log(error) // able to see error here
            return Observable.throw(error);
        });
}
Component code
enter code here

 try {
               
                   this.myservice.postSetup(this.result)
                        .subscribe(
                        (data)  => {
                            this.message  =  data
                            console.log("response data");
                            console.log(data);
                            if (this.message !== null || this.message !== undefined){
                                this.display = true;
                            }
                               
                            else{
                                this.errorDisplay = true;
                                let res = JSON.parse( data._body);
                                console.log("submit scenario error try");// not able to print message here
                                console.log(res)
                            }
                              

                        }
                        )
                         

                }
                catch (err) {
                   
                    this.errorDisplay = true;
                    let res = JSON.parse(err._body);
                    console.log("submit scenario error");// not able to print here
                    console.log(res)
                    this.sError = res['errorMsg'];
                    this.errorDesc = res['errorDesc'];
                }

注意:订阅功能中没有控件,但是,当使用 HTTP.get 调用其他 API 时,我可以订阅错误。请建议。这里缺少什么?

【问题讨论】:

    标签: angular rxjs observable http-post


    【解决方案1】:

    subscribe 函数也可以接受这样的对象。

    this.someService.makeRequest(someData).subscribe({ next: (successData) => {
    // Here only if is all went well
    }, error: (e)  => {
    // Here only is something went wrong
    }})
    

    如果在 observable 中抛出错误,error 将被调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 2015-11-22
      • 1970-01-01
      • 2023-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多