[英]Cannot get data from db using angular.js and php
我正在尝试使用PHP从MySQL数据库获取数据,并使用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];
}
在这里,我需要在成功函数内( ie-$http.get(..)
),控制台消息应显示所有请求的DB值。 请帮助我解决此问题。
成功和其他便利方法已从$ 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);
});
php://input
保存POST
数据,因此$http.get('js/edit.php'...)
请求应该是$http.post('js/edit.php'...)
请求使这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.