Angular JS $http.get不从PHP脚本接收数据


Angular JS $http.get does not receive data from PHP script

我使用的是angular的$http。get函数调用PHP脚本文件来检索数据

var $promise = $http.get("../php-bin/example.php",obj).success(function(data) {
              console.log(data);
            });

php应该只是从mysql数据库中获取数据,以找出一种方法来获取数据到我的应用程序

$user = json_decode(file_get_contents('php://input'));
$email = $user->email;
$pass = $user->pass;
$con = //a mysql_connection;
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
}
$validateEmail = "SELECT `Email` FROM `newUsers` WHERE `Email` = '$email' ";
if ($result = mysqli_query($con,$validateEmail)) {
     if ($result->num_rows == 1){
        $date = '2014-08-13'; 
        //$sql = "INSERT INTO newUsers (Email, CreationDate, UserRef, Type, id) VALUES ('$email','$date','$email','Host','$ssid')";
        $sql = "SELECT `email` FROM `newUsers` WHERE `hashpass` = '$pass' ";
        $result = mysqli_query($con,$sql);
        $row = mysqli_fetch_assoc($result);
        return $row;
        //I never receive this data in the angular app.
    } 
}
mysqli_close($con);
?>

谁能告诉我做这件事的正确方法

我看到你有一个return语句而不是echo语句。PHP输出中不打印return语句。函数外的return语句只有在包含以下文件时才有意义:

$returned = include('the_file_you_showed_here.php');
//you will hold the row here

return语句终止当前脚本返回到包含脚本(如果有的话),但不向输出发送任何值(这是die/exit的目的)。你应该:

  1. return更改为echo
  2. 如果你打算发送json数据,记得在任何实际的echo指令或非php内容之前发送header('Content-type: application/json')
  3. 请记住对数据进行编码:return json_encode($row) .

在你的PHP中确保你回显你的结果回显的结果被传递到变量

例如,如果你要从你的作用域

传入一个变量
function($scope,$http){
$scope.obj= "your variable";
$reuslt = http.get('yourlocation/yourfile.php?val='+obj');

你的PHP脚本

<?
$value = $_GET['val']

//not your variables have been passed in and you can use it to do your custom function wich ever way you like
and echo your result after wards
?>

希望能有所帮助

不能这样调用相对路径。该文件必须存在于公共目录或更低的目录。例如,如果您的所有文件都在/home/yourname/public中,则需要调用/php-bin/example.php,其中php-bin位于公共目录(/home/yourname/public/php-bin)中。