2016-02-25 40 views
0

我使用http.get從數據庫中獲取數據。我發送的參數與URL一起。但參數不能從該網址訪問。如何檢索使用http.get發送的參數

這裏是我的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; 
}; 
}]) 

這裏是我的data.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"; 
} 
?> 

它輸出的,即使我進入的地方 「進入不到位」。

這是檢索數據的正確方法嗎?

我通過任一地點或pincode,而不是兩個。會導致任何問題?

請大家幫忙。

+1

您正在使用GET在你的AJAX調用,但期望通過'file_get_contents(「php:// input」)''在PHP中進行POST。選擇其中之一。 – mitkosoft

+0

我是一個新手,我不知道如何獲得該參數。我應該用什麼來代替file_get_contents(「php:// input」)。我必須使用GET而不是POST。 – Abdulla

+1

要訪問GET參數,請使用$ _GET全局數組而不是php:// input,它是來自請求主體的輸入。 –

回答

0

解決了它。這是解決方案。

我用這兩個行代碼

$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);