我想知道如何使用http服务将参数从url传递到php。
服务:
getEmployeesById(data) {
let datas = JSON.stringify({'id':data});
return this.http.get('http://localhost/employees/?p=editEmployees', datas)
.map(response => response.json() );
}
组件:
ngOnInit() {
this.activatedRoute.queryParams.subscribe((queryParams: Params) => {
let userId = queryParams['id'];
this.employeesServices.getEmployeesById(userId)
.subscribe(data => {
this.result = data
});
});
}
PHP:http://localhost/employees/?p=editEmployees
$editEmployees = mysqli_query($con, "SELECT * FROM $employeesTable WHERE emp_id = '".$_GET['id']."' ");
$rows = mysqli_fetch_assoc($editEmployees);
$emp[] = $rows;
echo json_encode($emp);
-我创建了一个获取员工 ID 的 mysqli 查询。 -然后在组件中,我从 url param(id) 获取 emp id,如下所示
this.employeesServices.getEmployeesById(userId);
我的问题是,我不知道我传递的数据是否正确。
我使用
在 html 中显示数据{{ result.first_name }}
但我无法正确理解。
最佳答案
您可以在查询字符串中发送它,就像您已经将 p=editEmployees
作为查询字符串中的参数发送一样。您可以简单地在查询字符串中附加另一个参数,如下所示
getEmployeesById(data) { // as data is userId so simply append it
return this.http.get('http://localhost/employees/?p=editEmployees&id='+ data)
.map(response => response.json() );
}
关于php - Angular 4 - 如何使用参数将数据获取到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46067487/