在 Ionic 4 中从我的服务获取值到我的页面
Get Values from my Service to my Page in Ionic 4
现在我设法从我的 PHP-API 中获取了 DB-Data。现在我正在努力将它从我的服务发送到我的 page.ts
。不知何故,它一直未定义。
服务:
getData(dbargument:String) {
var headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
let fullheader = new HttpHeaders( headers );
let postData = dbargument;
let postDataJson = JSON.stringify(postData);
let header = new HttpHeaders();
let real_header : any = header.append('Content-Type', 'application/x-www-form-urlencoded');
this.http_post.post('URL is set here', postDataJson, real_header).subscribe(data => {
console.log("JSONSTRING: " + JSON.stringify(data));
return data;
});
}
Page.ts:
ngOnInit() {
/* Calling Data on Page load */
this.platform.ready().then(() => {
this.getActiveJobs();
console.log('Post initialized');
});
}
getActiveJobs() {
this.activeJobsID = this.activeJobs.userid;
this.activeJobsStr = "//Working SQL Statement is here";
this.activeJobsCount = this.activeJobs.getData(this.activeJobsStr);
console.log('Post worked');
console.log(this.activeJobsID);
}
我试图获取值的变量是 activeJobsCount
。
提前致谢!
编辑:
当前问题1
编辑 2:
看来问题出在以下部分:
this.http_post.post('http://kfz-expertus-portal.de/API/dbpost.php', postDataJson, real_header).subscribe(result => {
console.log("JSONSTRING: " + result);
return result;
});
the JSONSTRING is shown in the logs but i cant return the result to the page.ts
那是因为您没有从服务返回数据。
getData(dbargument:String) {
let tmp;
var headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
let fullheader = new HttpHeaders( headers );
let postData = dbargument;
let postDataJson = JSON.stringify(postData);
let header = new HttpHeaders();
let real_header : any = header.append('Content-Type', 'application/x-www-form-urlencoded');
this.http_post.post('URL is set here', postDataJson, real_header).subscribe(data => {
console.log("JSONSTRING: " + JSON.stringify(data));
this.temp = data;
});
return tmp;
}
不用等待数据,getData
函数瞬间从上到下returns undefined
正如它应该。您需要等待 http post 到 return,然后将其发送到您的页面。
getData(dbargument:String): Observable<any> {
// ... other
return this.http_post.post('URL is set here', postDataJson, real_header);
}
从您的服务调用函数并在您订阅 getData Observable 时查看结果
ngOnInit() {
this.getActiveJobs();
}
getActiveJobs() {
this.yourservice.getData(data).subscribe(result => {
// result here
});
}
我修好了。我只需要立即 return http 响应而不将其保存在结果中然后 return 然后。
现在我设法从我的 PHP-API 中获取了 DB-Data。现在我正在努力将它从我的服务发送到我的 page.ts
。不知何故,它一直未定义。
服务:
getData(dbargument:String) {
var headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
let fullheader = new HttpHeaders( headers );
let postData = dbargument;
let postDataJson = JSON.stringify(postData);
let header = new HttpHeaders();
let real_header : any = header.append('Content-Type', 'application/x-www-form-urlencoded');
this.http_post.post('URL is set here', postDataJson, real_header).subscribe(data => {
console.log("JSONSTRING: " + JSON.stringify(data));
return data;
});
}
Page.ts:
ngOnInit() {
/* Calling Data on Page load */
this.platform.ready().then(() => {
this.getActiveJobs();
console.log('Post initialized');
});
}
getActiveJobs() {
this.activeJobsID = this.activeJobs.userid;
this.activeJobsStr = "//Working SQL Statement is here";
this.activeJobsCount = this.activeJobs.getData(this.activeJobsStr);
console.log('Post worked');
console.log(this.activeJobsID);
}
我试图获取值的变量是 activeJobsCount
。
提前致谢!
编辑:
当前问题1
编辑 2:
看来问题出在以下部分:
this.http_post.post('http://kfz-expertus-portal.de/API/dbpost.php', postDataJson, real_header).subscribe(result => {
console.log("JSONSTRING: " + result);
return result;
});
the JSONSTRING is shown in the logs but i cant return the result to the page.ts
那是因为您没有从服务返回数据。
getData(dbargument:String) {
let tmp;
var headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
let fullheader = new HttpHeaders( headers );
let postData = dbargument;
let postDataJson = JSON.stringify(postData);
let header = new HttpHeaders();
let real_header : any = header.append('Content-Type', 'application/x-www-form-urlencoded');
this.http_post.post('URL is set here', postDataJson, real_header).subscribe(data => {
console.log("JSONSTRING: " + JSON.stringify(data));
this.temp = data;
});
return tmp;
}
不用等待数据,getData
函数瞬间从上到下returns undefined
正如它应该。您需要等待 http post 到 return,然后将其发送到您的页面。
getData(dbargument:String): Observable<any> {
// ... other
return this.http_post.post('URL is set here', postDataJson, real_header);
}
从您的服务调用函数并在您订阅 getData Observable 时查看结果
ngOnInit() {
this.getActiveJobs();
}
getActiveJobs() {
this.yourservice.getData(data).subscribe(result => {
// result here
});
}
我修好了。我只需要立即 return http 响应而不将其保存在结果中然后 return 然后。