0

现在我设法从我的 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;
 });

} 

页面.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

4

3 回答 3

0

我修好了它。我只需要立即返回 http-response 而不将其保存在结果中然后返回。

于 2019-10-14T10:09:58.167 回答
0

您无需等待数据,该getData函数会立即从上到下并按undefined原样返回。您需要等待 http 帖子返回,然后将其发送到您的页面。

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
  });
}
于 2019-10-10T14:35:15.240 回答
0

那是因为您没有从服务返回数据。

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;
} 
于 2019-10-10T13:28:06.387 回答