douhuangjian9627 2014-08-17 00:52
浏览 49
已采纳

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

I am using angular's $http.get function to call a PHP script file to retrieve data

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

              console.log(data);
            });

The php is supposed to just get data from a mysql db to figure out a way to get data into my app

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

Could some one point me to the correct way to do this.

  • 写回答

3条回答 默认 最新

  • doudou6719 2014-08-17 05:49
    关注

    I see you have a return statement instead of an echo statement. A return statement is not printed in the PHP output. 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
    

    A return statement kills the current script returning to an includer script (if any), but does not send any value to the output (that's the purpose of die/exit). You should:

    1. Change the return to echo.
    2. Remember to have a header('Content-type: application/json') sent if you intend to send json data, before any actual echo instruction or non-php content.
    3. Remember to encode the data: return json_encode($row).
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧