首页 > 解决方案 > Getting HttpErrorResponse in ionic3 for success response

问题描述

I am new to Ionic/Angular. Able to see success response from server in tag error.text. Please correct me where I am being wrong.

Code as follows.

    callSetMpin(otprefno){
        alert(otprefno);
        this.panRegistration.setMpin(otprefno)
        .subscribe(
          response =>{
            this.setMpinResponse = response;
            console.log(this.setMpinResponse);
            alert("Success call");
          },
          err => {
            console.log("Oops!-->"+JSON.stringify(err));
            alert("We apologize this facility is temporarily unavailable.Please try later. ");
        }
        );
      }

setMpin(otprefno){
    let data = "SessionId=1990009769OKYLLDDP&RQDeviceId=9876543217&RQRefNo="+otprefno+"&RQLoginUserId=101068073&RQDeviceFormat=Tablet&RQOperationId=USRREGREQ&RQDeviceFamily=Android&RQTransSeq=01&RQOTP=123123&RQClientAPIVer=1.0&RQMPIN=MTExMTEx";
    console.log(otprefno);
    console.log(data);
    return this.http.post(this.url+'/rbl/Registration2', data, 
    {headers:{'Content-Type':'application/json'}}
    );
  }

Getting error as follows:

{
  "headers": {
    "normalizedNames": {

    },
    "lazyUpdate": null
  },
  "status": 200,
  "statusText": "OK",
  "url": "http://10.80.45.72:7080/rbl/Registration2",
  "ok": false,
  "name": "HttpErrorResponse",
  "message": "Http failure during parsing for http://10.80.45.72:7080/rbl/Registration2",
  "error": {
    "error": {

    },
    "text": "<NS1:RBL xmlns:NS1=\"http://www.w3.org/2001/XMLSchema_Response_Registration2\"><NS1:HeaderEnv><NS1:RemoteIP></NS1:RemoteIP><NS1:ServerAPI></NS1:ServerAPI><NS1:RBLOperationId></NS1:RBLOperationId><NS1:LANGUAGE></NS1:LANGUAGE></NS1:HeaderEnv><NS1:SessionEnv><NS1:SessionId></NS1:SessionId><NS1:LoggedinUser></NS1:LoggedinUser><NS1:CoreSessionId1></NS1:CoreSessionId1><NS1:CoreSessionId2></NS1:CoreSessionId2><NS1:Token></NS1:Token><NS1:APPID></NS1:APPID><NS1:APPVER>2.0</NS1:APPVER><NS1:DeviceType>AndroidPhone</NS1:DeviceType></NS1:SessionEnv><NS1:RequestParams><NS1:RQOperationId>USRREGREQ</NS1:RQOperationId><NS1:RQTransSeq>01</NS1:RQTransSeq><NS1:RQLoginUserId>101068073</NS1:RQLoginUserId><NS1:RQMPIN>MTExMTEx</NS1:RQMPIN><NS1:RQDateTime></NS1:RQDateTime><NS1:RQClientAPI version=\"1.0\"/><NS1:RQDeviceFamily dname=\"Android\"/></NS1:RequestParams><NS1:STATUS><NS1:CODE>0</NS1:CODE><NS1:SEVERITY>SUCCESS</NS1:SEVERITY><NS1:MESSAGE>Registration Successful</NS1:MESSAGE><NS1:customerid>101068073</NS1:customerid></NS1:STATUS></NS1:RBL>"
  }
}

How can I get this in success response instead of err response?

标签: ionic3

解决方案


下面是正确的代码。添加了 responseType 并删除了 Content-Type。

setMpin(otprefno){
    let data = "SessionId=1990009769OKYLLDDP&RQDeviceId=9876543217&RQRefNo="+otprefno+"&RQLoginUserId=101068073&RQDeviceFormat=Tablet&RQOperationId=USRREGREQ&RQDeviceFamily=Android&RQTransSeq=01&RQOTP=123123&RQClientAPIVer=1.0&RQMPIN=MTExMTEx";
    console.log(otprefno);
    console.log(data);
    return this.http.post(this.url+'/rbl/Registration2', JSON.stringify(data), {
       responseType: 'text' 
    });
  }

推荐阅读