如何使用JSON数据来自Angular2中的PHP文件?(how to use JSON data Coming from PHP file in Angular2?)

编程入门 行业动态 更新时间:2024-10-24 16:31:55
如何使用JSON数据来自Angular2中的PHP文件?(how to use JSON data Coming from PHP file in Angular2?) export class LoginComponent { getdata : string; public data; username : any ; password : any ; constructor(private http: Http){} login() { var headers= new Headers({'Content-Type' : 'application/x-www-form-urlencoded '}); var body = JSON.stringify({ user : this.username, pwd : this.password, }) this.http.post('.../pos_system/Widgets/Login.php',body, { headers:headers; }) .map(res => res.json()) .map(res => { if(res.success) { this.msg="Login Complete"; } else{ this.msg1="username and password is wrong"; } }) .subscribe( data =>this.getdata = JSON.stringify(data), err => console.error(err), () => console.log('done')); } }

这是我的Angular2部分,我在res获取来自php文件的JSON数据。现在我想在我的angualr2部分使用这个响应。我想使用json data中用户的显示用户名。如何在angular2中做到这一点?

export class LoginComponent { getdata : string; public data; username : any ; password : any ; constructor(private http: Http){} login() { var headers= new Headers({'Content-Type' : 'application/x-www-form-urlencoded '}); var body = JSON.stringify({ user : this.username, pwd : this.password, }) this.http.post('.../pos_system/Widgets/Login.php',body, { headers:headers; }) .map(res => res.json()) .map(res => { if(res.success) { this.msg="Login Complete"; } else{ this.msg1="username and password is wrong"; } }) .subscribe( data =>this.getdata = JSON.stringify(data), err => console.error(err), () => console.log('done')); } }

This is my Angular2 part here i m getting JSON data from php file in res.Now I want to use this response in my angualr2 part.I want to use display username of the user which is in json data.so how to do that in angular2?

最满意答案

只需进行此更改即可,例如this.msg=res.username; 这将在消息中显示用户名。 注意:你需要在php文件中执行此操作。

$data=array(); $df=json_decode(file_get_contents("php://input")); $nam=$df->user; $pws=$df->pwd; $select=mysql_query("SELECT * FROM userData WHERE username='$nam' AND password='$pws'")or mysql_error(); $sql=mysql_num_rows($select); if($sql>0) { while($row=mysql_fetch_array($select)) { $data['success']=true; $user=$row['username']; $data['username']=$user; } } echo json_encode($data);

it's simple just make this change, for Example this.msg=res.username; this will display username in message. note: you need to do this in php file.

$data=array(); $df=json_decode(file_get_contents("php://input")); $nam=$df->user; $pws=$df->pwd; $select=mysql_query("SELECT * FROM userData WHERE username='$nam' AND password='$pws'")or mysql_error(); $sql=mysql_num_rows($select); if($sql>0) { while($row=mysql_fetch_array($select)) { $data['success']=true; $user=$row['username']; $data['username']=$user; } } echo json_encode($data);

更多推荐

本文发布于:2023-07-31 19:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1347233.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   文件   数据   JSON   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!