【发布时间】:2019-11-30 08:03:31
【问题描述】:
我正在尝试使用 Angular 和 json 将数据库中的一些数据显示到我的 html 页面,但是即使设法读取我的数据库,浏览器也不会显示我的数据。有人可以解释为什么会这样吗?
我在这个网站上看了几个答案,但没有一个能解决这个问题,甚至从不同的浏览器和从 unix 切换到 windows。
这是我的控制器
myApp.controller('postCtrl',function($scope){
$.ajax({
type: 'GET',
url: './webservices/post.php',
dataType : 'json',
success : function(response){
$scope.locali = response.info;
$scope.posts = response.info;
console.log($scope.locali);
},
fail: function(result){
$("#messaggiodierrore").html("query non riuscita! ");
}
});
console.log($scope.locali);
});
这是我的html
<div class="col-md-8" ng-controller = "postCtrl">
<table class="table table-hover">
<thead>
<tr>
<th>Locale </th>
<th>Indirizzo</th>
<th>città</th>
<th>attrezzature</th>
</tr>
</thead>
<tbody >
<tr ng-repeat="locale in locali track by $index" >
<td>{{locale.nomeLocale}} </td>
<td>{{locale.indirizzo}} </td>
<td>{{locale.citta}} </td>
<td>{{locale.attrezzature}} </td>
<td><a class="btn btn-default" href="#!rispondi">Rispondi</a>
</td>
</tr>
</tbody>
</table>
</div>
这是我的 php(正在工作)
<?php
include_once('config.php');
$sql= "SELECT * FROM locale";
$query = mysqli_query($conn,$sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR);
if(mysqli_num_rows($query) > 0){
while($row = mysqli_fetch_assoc($query)){
$locale = $row['NomeLocale'];
$indirizzo = $row['indirizzo'];
$citta = $row['citta'];
$attrezzature = $row['attrezzature'];
$result[] = array('nomeLocale'=>$locale, 'indirizzo'=>$indirizzo,'citta'=>$citta,'attrezzature'=>$attrezzature);
}
$json = array('info' => json_encode($result));
} else {
$json = array('status'=> 0, 'msg' =>'nessun locale trovato');
}
mysqli_close($conn);
header("Content-type: application/json");
echo json_encode($json);
?>
谷歌浏览器控制台给我发这个:
[{"nomeLocale":"asdf","indirizzo":"asdf","citta":"asdf","attrezzature":"0"}]
这就是我的数据库中的内容,但我无法理解为什么它没有显示在我的 html 表格中。`
【问题讨论】:
-
请分享 Angular js 完整代码
-
使用 AngularJS $http 服务。 jQuery
.ajax方法没有与 AngularJS 框架集成。只有在 AngularJS 执行上下文中应用的操作才能受益于 AngularJS 数据绑定、异常处理、属性监视等。
标签: javascript php html json angularjs