-1
从app.component.ts
文件,我在[Object Object]
从alert(this.datas);
但alert(this.datas.ReportDtDataForMAPP);
形式取回数据给出空白结果作为一个结果是,当我尝试访问该报告的网址从UI,我得到这个错误:Cannot read property 'URL' of undefined
。无法读取的未定义的属性“URL” - 访问JSON对象
有人可以提供一个解决方案吗?
下面是必要的文件:
app.component.ts类(节选)
//...
//Lines of code
//...
this.myMobileDataService.checkAuthorization(this.enterpriseId,v2Token)
.subscribe(
response => {
this.datas = response;
JSON.stringify(this.datas);
alert(this.datas);
alert(this.datas.ReportDtDataForMAPP);
if(this.datas == null || this.datas == ""){
this.nav.setRoot(this.accessDeniedPage, {showFooter : false});
} else {
this.nav.setRoot(this.homePage, {showFooter : false});
}
},
err => {
console.log("Error here!:"+err);
}
);
},
//...
//Lines of code
//...
OpenReport(report : any)
{
let list = this.datas.ReportDtDataForMAPP;
let newList = list.filter((t) => t.SubReport == report);
JSON.stringify(newList)
this.myURL = newList[0].URL;
this.openUrl(this.myURL);
}
//...
//Lines of code
//...
app.html(节选)
//...
//Lines of code
//...
<ion-grid>
<ion-row>
<ion-col class="reportHeader reportHeaderPadding"><strong>AAA</strong></ion-col>
</ion-row>
<ion-row text-center>
<ion-col (click)="OpenReport('MURLAAA')" tappable><img src = "assets/icon/AAA.png" alt = "Image Not Available" class = "homeImages"></ion-col>
<ion-col (click)="OpenReport('MURLBBB')" tappable> <img src = "assets/icon/BBB.png" alt = "Image Not Available" class = "homeImages"></ion-col>
<ion-col (click)="OpenReport('MURLCCC')" tappable> <img src = "assets/icon/CCC.png" alt = "Image Not Available" class = "homeImages"></ion-col>
</ion-row>
<ion-row text-center class="addBorder">
<ion-col (click)="OpenReport('MURLAAA')" tappable class="reportName">AAA</ion-col>
<ion-col (click)="OpenReport('MURLBBB')" tappable class="reportName">BBB</ion-col>
<ion-col (click)="OpenReport('MURLCCC')" tappable class="reportName">CCC</ion-col>
</ion-row>
//...
//Lines of code
//...
myMobileDataService。 ts class
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { Headers,RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';
import { Platform } from 'ionic-angular';
@Injectable()
export class myMobileDataService {
constructor(private http: Http, private platform: Platform) {}
public checkAuthorization(_enterpriseID: string, _token : any) : Observable<any>{
let details = {
"EntId" : "_enterpriseID"
};
let body = JSON.stringify(details);
let headers = new Headers(
{
'Content-Type': 'application/json',
'Authorization' : 'bearer ' + _token
}
);
let options = new RequestOptions({headers:headers});
return this.http.post('myURL',body,options)
.map((res) => res.json())
}
}
HTTP POST请求以这种形式提供了数据:
ReportDtDataForMAPP:[
{
"MainReport": "AAA",
"SubReport": "aaa",
"URL": "https://[email protected]"
},
{
"MainReport": "BBB",
"SubReport": "bbb",
"URL": "https://[email protected]"
},
{
"MainReport": "CCC",
"SubReport": "ccc",
"URL": "https://[email protected]"
}
]
相反警报(this.datas)的',','console.log'的'this.datas'序列化JSON来检查服务真的返回你正在寻找的东西对于。 –
@SayanPal这也是打印[对象对象] – Debo
这看起来不正确,因为'console.log(JSON.stringify(this.data))'应该显示'this.data'的json表示。你确定你在做这个吗? –