douchensou6969 2015-09-24 06:12
浏览 46

无法使用angular.js和php从db获取数据

I am trying to get data from MySQL DB using PHP and show them using Angular.js . But here I am getting the following message in browser's console and became unable to fetch the data.

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

I am explaining my code below.

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];
}

Here I need inside the success function(i.e-$http.get(..)) the console message should display all requested DB values. Please help me to resolve this issue.

  • 写回答

2条回答 默认 最新

  • dongyong2906 2015-09-24 06:42
    关注

    success and other convenience methods are removed from $http. You need to use the promise-like API on $http like below

            // 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);
            });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R