【问题标题】:Cannot get data from db using angular.js and php无法使用 angular.js 和 php 从 db 获取数据
【发布时间】:2015-09-24 06:12:17
【问题描述】:

我正在尝试使用 PHP 从 MySQL DB 获取数据并使用 Angular.js 显示它们。但是在这里我在浏览器的控制台中收到以下消息并且无法获取数据。

response <br />
<b>Notice</b>:  Trying to get property of non-object in <b>C:\xampp\htdocs\phpdemo\js\edit.php</b> on line <b>3</b><br />
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>C:\xampp\htdocs\phpdemo\js\edit.php</b> on line <b>9</b><br />
false

我在下面解释我的代码。

edit.php:

<?php
$data = json_decode(file_get_contents("php://input"));
$user_id = mysql_real_escape_string($data->user_id);
$con = mysql_connect('localhost', 'root', '');
mysql_select_db('dbtuts', $con);
$qry="SELECT * FROM users WHERE user_id=".$user_id;
$qry_res = mysql_query($qry);
$r = array();
if( $qry_res->num_rows>0){
while($row = $qry_res->fetch_assoc()){
$r[] = $row;
}
}
$res = json_encode($qry_res);
echo $res;
?>

update.js:

var app=angular.module("edit_data", []);
app.controller("updateController",function($scope,$http,$location){
    $scope.errors = [];
    $scope.msgs = [];
    var id=gup( "edt_id" );
    console.log("id is :",id);
    $http.get('js/edit.php',{"user_id":id}).success(function(response){
        console.log('response',response);
    });
    $scope.update_data=function(){
        $http.post('js/update.php',{"first_name":$scope.first_name,"last_name":$scope.last_name,"city":$scope.city}
        ).success(function(data, status, headers, config){
            if(data.msg!=''){
            $scope.msgs.push(data.msg);
            }else{
                $scope.errors.push(data.error);
            }
        }).error(function(data, status) { // called asynchronously if an error occurs
               // or server returns response with an error status.
              $scope.errors.push(status);
        });
    }
});
function gup( name ) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

这里我需要在成功函数(i.e-$http.get(..)) 中,控制台消息应该显示所有请求的数据库值。请帮我解决这个问题。

【问题讨论】:

  • 也许 $data 在 3 号线上不是一个快乐的露营者。把它扔掉
  • @Drew :我删除了这个,但输出没有出现。
  • 我想通过“转储”@Drew 意味着创建一个var_dump 以查看变量中的内容,例如:var_dump($data);。不要删除它。见这里var_dump
  • 是的@xpy 抱歉
  • 跟随本教程Angular with php mysql

标签: php mysql angularjs


【解决方案1】:

成功和其他便利方法已从 $http 中删除。您需要在 $http 上使用类似 Promise 的 API,如下所示

        // this will fire a call to js/edit.php?user_id=value
        $http.get('js/edit.php',{"user_id":id})
             .then(function(response){
              // data property of response object will have actual response from server
              console.log('response'+response.data);
        });

【讨论】:

  • @Arkantos:我添加了你的行,但它显示控制台输出为responsefalse
  • 您是否在开发工具 (F12) 网络选项卡中检查了该 Ajax 调用的实际响应?它说什么?
  • 那么你需要看看你的 PHP 脚本。我们希望我们的 PHP 服务器为我们在 AngularJS 中发出的 GET 请求发送一些响应。我不了解 PHP,但请查看 xpy 的答案,看看它是否有助于解决您的 PHP 问题。
【解决方案2】:

php://input 持有POST 数据,因此$http.get('js/edit.php'...) 请求应该是$http.post('js/edit.php'...) 请求,以使其工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-17
    • 2021-04-16
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    相关资源
    最近更新 更多