我想从API检索离子数据。首先,我遇到了CORS问题,无法从原始端口以外的其他设备检索数据。我试图使用http native来检索设备上的数据。我有从API获取标头的问题。我不允许修改服务器。但是,我最终遇到如下错误:请指导我如何在ionic中使用http native。
(in promise): Error: advanced-http: header values must be strings
Error: advanced-http: header values must be strings
我尝试使用以下代码:
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()});
}
数据未显示。
答案 0 :(得分:1)
这是我的下方标题:
getMcsHeadersString() {
var cheaders = {};
for (let key in this.mcs.mobileBackend.getHttpHeaders()) {
cheaders[key]= this.mcs.mobileBackend.getHttpHeaders()[key];
}
//console.log("STring_HeaderKey_"+cheaders['_keys']+"HeaderValue_"+cheaders['_values']);
delete cheaders['_keys'];
delete cheaders['_values'];
return cheaders;
}