【问题标题】:jQuery ajax not displaying data from php to a table with angularjs and jsonjQuery ajax 没有将 php 中的数据显示到带有 angularjs 和 json 的表中
【发布时间】: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


【解决方案1】:

Angular 提供了两种不同的 API 来处理 Ajax 请求。他们是

  • $resource(与服务器的更高级别交互)
  • $http(与服务器的低级交互)

在控制器 ( postCtrl ) 中创建新模块并设置 AJAX 请求。

Path – 在 url 中传递文件名“你的 url”并设置获取方法。

Success – AJAX 成功回调句柄,此时 store reponse.infoto $scope.locali

在 HTML 结束时使用 ng-repeat 指令显示数据。

试试这个:

<script>
var fetch = angular.module('myapp', []);

fetch.controller('postCtrl', ['$scope', '$http', function ($scope, $http) {
 $http({
  method: 'get',
  url: 'webservices/post.php'
 }).then(function successCallback(response) {
  // Store response data
  $scope.locali = response.info;
 });
}]);

</script>

【讨论】:

  • 好的,现在我用你的代码更新了我的控制器,浏览器的控制台没有显示任何东西。在面板网络 > 响应中,我看到它看到了我想在桌子上显示的字符串,但这仍然没有发生
  • 它只会在控制台中显示响应,当你添加console.log($scope.locali);
猜你喜欢
  • 2011-10-25
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-08
  • 1970-01-01
  • 2014-02-11
相关资源
最近更新 更多