我正在使用http.get从数据库中获取数据。我正在发送参数和网址。但无法从该 URL 访问参数。
这是我的Angularjs控制器代码:
app.controller('ListCtrl', ['$scope', '$http', '$location', '$window', function($scope,$http,$location,$window){
$scope.data = {};
$scope.getdata = function(){
$scope.datas = [];
$http({
url: "http://localhost/angular/data.php",
method: "GET",
params: {'place':$scope.data.place,'pincode':$scope.data.pincode}
})
.success(function(data,status,headers,config){
$scope.datas=data;
console.log($scope.datas);
$scope.navig('/show.html');
})
.error(function(){
alert("failed");
});
};
$scope.navig = function(url){
$window.location.href = url;
};
}])
这是我的数据.php文件
<?php
header("Access-Control-Allow-Origin: *");
$postdata = file_get_contents("php://input");
$data= json_decode($postdata);
$place = mysql_real_escape_string($data->place);
$pincode = mysql_real_escape_string($data->pincode);
//$place = $_GET[$data->place];
if ($place){
$connection = mysqli_connect("localhost","root","","building") or die("Error " . mysqli_error($connection));
$sql = "SELECT * from details ";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$detail = array();
while($row =mysqli_fetch_assoc($result))
{
$detail[] = $row;
}
echo json_encode($detail);
mysqli_close($connection);
}
else {
echo "no place entered";
}
?>
即使我进入该地点,它也输出为"未进入该地点"。
这是检索数据的正确方法吗?
我传递的是地点或密码,而不是两者兼而有之。 这会导致任何问题吗?
请帮忙。
解决了。这是解决方案。
我使用了这两行代码
$place = $_GET['place'];
$pincode= $_GET['pincode'];
而不是这 4 行代码,它实际上用于检索随 http.post 发送的数据
$postdata = file_get_contents("php://input");
$data= json_decode($postdata);
$place = mysql_real_escape_string($data->place);
$pincode = mysql_real_escape_string($data->pincode);