我是AngularJS的新手。我只想从数据库中获取数据并将其存储在文本框中。
这是我的代码:
$scope.sample = function(){
$http.post('query?action=get',{
})
.then(function(response){
$scope.info = response;
})
.catch(function(response){
alert("error");
});
};
// call the function
$scope.sample();
并在PHP中:
$action = $_GET['action'];
if($action == 'get'){
$sql = "SELECT * FROM tableSomething";
$sql_res = $con->query($sql);
$data = array();
while($row = mysqli_fetch_array($sql_res)){
$data[] = $row;
}
echo json_encode($data);
}
并在我的HTML文件中
<input type="text" ng-model="info.firstname" >
告诉我哪里出错了。谢谢!
你response
是一个对象。
首先,您的服务器请求是GET而不是POST。
因此,改变$http.post('query?action=get',{
与$http.get('query?action=get',{
如果没有其他的代码问题你得到data
的response
。
替换$scope.info = response;
为$scope.info = response.data;
您的示例代码缺少的定义$scope
,因此我无法对其进行全面测试。
希望我能解决您的问题
更新1:
好的。AngularJS太有见识了。
它看到响应内容具有JSON结构,并将其作为对象返回。
让我们字符串化:
.then(function(response){
$scope.info = JSON.stringify(response);
})
并改变你的
<input type="text" ng-model="info.firstname" >
至
<input type="text" ng-model="info" >
这是一个JSFiddle示例
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句