I would like to retrieve data from API to ionic. First, I was facing issue of CORS and unable to retrieve data from different devices except original port. I was trying to use http native to retrieve data on device. I have a problem of getting header from API. I am not allowed to modify server. However, I ended up with error as below : Please guide me how to use http native in ionic.
(in promise): Error: advanced-http: header values must be strings
Error: advanced-http: header values must be strings
I have tried with following code:
login(username: String, password: String) {
if(username === undefined || username === '' || password === undefined || password === null){
presentAlert("No username or password", this.alertController);
return;
}
this.mcs.mobileBackend.setAuthenticationType(this.mcs.AUTHENTICATION_TYPES.oauth);
this.mcs.mobileBackend.authorization.authenticate(username, password).then(
() => {
console.log("Test");
this.http.get(this.mcs.mobileBackend.getCustomCodeUrl(mcsConfig.environment + '_api_care/lovs?code_table_name=activity_code'), {}, {headers:careheader})
.then(data => {
console.log(data.status);
console.log(data.data); // data received by server
console.log(data.headers);
})
.catch(error => {
console.log(error.status);
console.log(error.error); // error message as string
console.log(error.headers);
});
}
).then(() => {
console.log('test2' + username + password);
this.getUserRole();
this.authenticationState.next(true);
}).catch(err => {
console.log(err,Headers);
presentAlert(err, this.alertController);
});
}
getHeader(){
let headers = new Headers();
this.createAuthorizationHeader(headers);
headers.append('Content-Type', 'application/json');
}
getUserRole() {
return this.http.get(this.mcs.mobileBackend.getPlatformUrl('users/me'), {},{ }).then(data => {
console.log(data); console.log(data.headers);
});
}
getReportList(offset: number = 0, limit: number = mcsConfig.rowLimit) {
let queryParam = '?offset=' + (offset * limit) + '&limit=' + limit;
//return this.mcs.mobileBackend.customCode.invokeCustomCodeJSONRequest(mcsConfig.environment + '_api_care/my/incidents' + queryParam, 'GET', null);
return this.http.get(this.mcs.mobileBackend.getCustomCodeUrl(mcsConfig.environment + '_api_care/my/incidents'+ queryParam), {},{headers: this.getMcsHeaders()});
}
The data is not shown.