Angular 在Ionic 4中从我的服务到我的页面获取值
现在我设法从我的PHP-API中获得了DB数据。现在我正努力将它从我的服务发送到我的Angular 在Ionic 4中从我的服务到我的页面获取值,angular,typescript,ionic4,Angular,Typescript,Ionic4,现在我设法从我的PHP-API中获得了DB数据。现在我正努力将它从我的服务发送到我的页面.ts。不知怎的,它一直没有定义 服务: getData(dbargument:String){ 变量头={ “内容类型”:“应用程序/x-www-form-urlencoded” }; 让fullheader=新的HttpHeaders(headers); 设postData=dbargument; 让postDataJson=JSON.stringify(postData); let header=新的H
页面.ts
。不知怎的,它一直没有定义
服务:
getData(dbargument:String){
变量头={
“内容类型”:“应用程序/x-www-form-urlencoded”
};
让fullheader=新的HttpHeaders(headers);
设postData=dbargument;
让postDataJson=JSON.stringify(postData);
let header=新的HttpHeaders();
let real_header:any=header.append('Content-Type','application/x-www-form-urlencoded');
this.http\u post.post('URL设置在这里',postDataJson,real\u头)。订阅(数据=>{
log(“JSONSTRING:+JSON.stringify(数据));
返回数据;
});
}
Page.ts:
ngOnInit(){
/*在页面加载时调用数据*/
this.platform.ready()。然后(()=>{
这是getActiveJobs();
log('Post initialized');
});
}
getActiveJobs(){
this.activeJobsID=this.activeJobs.userid;
this.activejobstr=“//工作SQL语句在此”;
this.activeJobsCount=this.activeJobs.getData(this.activejobstr);
console.log(“后期处理”);
console.log(this.activeJobsID);
}
我试图获取其中的值的变量是activeJobsCount
提前谢谢
编辑:
当前问题
编辑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
函数会在瞬间自上而下地返回未定义的。您需要等待http post返回,然后将其发送到您的页面
getData(dbargument:String):可观察{
//……其他
返回this.http_post.post('URL在这里设置',postDataJson,real_头);
}
从服务调用函数,并在订阅getData Observable时查看结果
ngOnInit(){
这是getActiveJobs();
}
getActiveJobs(){
this.yourservice.getData(data.subscribe)(结果=>{
//结果在这里
});
}
我把它修好了。我只需立即返回http响应,而不将其保存在结果中,然后返回即可。您好,首先谢谢!我将其更改为您的代码,同时将“this.temp=data”更改为tmp=data。我想你的意思是tmp和“this.”不应该是必要的,因为这不是实例,对吗?是的,你是对的,从代码中删除“this”。我希望它能起作用,可惜不行。看起来返回的tmp没有值;console.log(“JSONSTRING:+JSON.stringify(data))是否正在打印任何内容?另外,最好在页面中使用subscribe,比如@tomas vancoillie suggestedthank you:)我这样做并得到以下错误:core.js:9110错误:Uncaught(承诺中):TypeError:无法读取未定义TypeError的属性“subscribe”:无法读取未定义TypeError的属性“subscribe”。我现在正在寻找一个解决方案。您的http\U post变量是注入的HttpClient吗?是的,构造函数如下:constructor(private http\u post:HttpClient){}
刚刚发现“`this.activeJobs.getData(this.activejobstr)``是未定义的。您应该检查是否从post返回任何数据。您的PHPAPI是否发送回数据?如果您愿意,可以向邮递员查询()