【问题标题】:AngularJS: get JSON from php scriptAngularJS:从 php 脚本获取 JSON
【发布时间】:2017-04-10 16:17:23
【问题描述】:

我正在 w3schools 尝试以下 AngularJS 教程。

Link to w3schools tutorial

我更改了脚本以使其与我的一个 php 脚本一起工作:

<!DOCTYPE html>
<html >
<style>
table, th , td  {
  border: 1px solid grey;
  border-collapse: collapse;
  padding: 5px;
}
table tr:nth-child(odd) {
  background-color: #f1f1f1;
}
table tr:nth-child(even) {
  background-color: #ffffff;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl">

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.name }}</td>
  </tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
       $http.get("http://wirtschaftsinformatik.web2page.ch/selectModule.php")
//my url
       .then(function (response) {$scope.names = response.data.records;});
    });
    </script>

    </body>
    </html>

这实际上是行不通的。我猜是因为 php 文件,这里是代码:

<?PHP
    //Verbindung zur Datenbank
    include "inc_mysql.php";

    //SQL-String
    $select = "SELECT * FROM module ORDER BY name ASC";

    // Codierung
    mysql_set_charset("utf8");

    //SQL-Befehl in Variable speichern
    $sql = mysql_query($select);

    //Array erstellen
    $jsonArray = array();

    while ($row = mysql_fetch_array($sql)){ 
        $jsonArray[] = $row;
    }

    echo json_encode($jsonArray, JSON_UNESCAPED_UNICODE);   
?>

你知道缺少什么吗?

这是 JSON 输出

[{"0":"12","id":"12","1":"Betriebsbuchhaltung","name":"Betriebsbuchhaltung","2":"2016-10-19 15:44:16","timestamp":"2016-10-19 15:44:16"},{"0":"13","id":"13","1":"Datenbanken","name":"Datenbanken","2":"2016-10-28 20:35:49","timestamp":"2016-10-28 20:35:49"},{"0":"6","id":"6","1":"Logistik","name":"Logistik","2":"0000-00-00 00:00:00","timestamp":"0000-00-00 00:00:00"},{"0":"14","id":"14","1":"Statistik","name":"Statistik","2":"2016-11-02 15:13:13","timestamp":"2016-11-02 15:13:13"},{"0":"1","id":"1","1":"System Engineering","name":"System Engineering","2":"0000-00-00 00:00:00","timestamp":"0000-00-00 00:00:00"}]

【问题讨论】:

  • 什么不起作用?表不显示记录?
  • 是的,表格没有显示记录。
  • 你能发布json吗
  • @Sajeetharan 我添加了它。

标签: javascript php angularjs json


【解决方案1】:

返回的json没有records属性,这里有一个简单的解决方案:

echo json_encode(["records"=>$jsonArray], JSON_UNESCAPED_UNICODE); 

【讨论】:

【解决方案2】:

HTML

 <div ng-controller="listController">
    <table>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Time</th>
      </tr>
      <tr ng-repeat="person in names">
        <td>{{person.id}}</td>
        <td>{{person.name}}</td>
        <td>{{person.timestamp}}</td>
      </tr>
    </table>
  </div>

DEMO

【讨论】:

  • 非常感谢!它的作品很棒!我看到你创建了一个 JSON 文件。有没有办法直接从 php 脚本中获取它?我
  • 是的,您可以使用 get http 调用并执行相同操作。如果有帮助则标记为答案
  • 谢谢!你的意思是什么?我输入了wirtschaftsinformatik.web2page.ch/selectModule.php,但没有用。
  • @EroStefano 你不能直接从角度获取数据,你需要在 php 中这样做。检查此链接stackoverflow.com/questions/25064570/…
猜你喜欢
  • 2017-06-26
  • 2017-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多