kajal kajal - 1 year ago 84
JSON Question

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 => {
this.msg="Login Complete";
this.msg1="username and password is wrong";
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
.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?

Answer Source

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.



    $select=mysql_query("SELECT * FROM userData WHERE username='$nam' AND password='$pws'")or mysql_error();


    echo json_encode($data);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download