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

时间:2014-08-17 00:52:18

标签: javascript php mysql angularjs

我正在使用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);
?>

有人可以指出我这样做的正确方法。

3 个答案:

答案 0 :(得分:4)

我看到你有return语句而不是echo语句。 PHP输出中不打印return语句。 A return statement outside a function has only sense when you include the file

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

return语句kills the current script返回到包含者脚本(如果有),但不向输出发送任何值(这是die / {的目的{1}})。你应该:

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

答案 1 :(得分:3)

在你的php中

确保你回应你的结果,回声结果传递到你的变量

例如,如果您从范围传递一个varibale,那么

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
?>

希望这会有所帮助

答案 2 :(得分:1)

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