【问题标题】:parsing RESTFUL JSON to html将 RESTFUL JSON 解析为 html
【发布时间】:2015-09-21 20:00:16
【问题描述】:

我在我的视图中有这个,以便人们去示例,查看/1 我通过 restful 获取该用户的所有数据,我用表格在 html 中解析它,我使用 cakephp,我不想用 angularJS,有一个 JQuery 库可以做这个工作,你知道名字吗?

 <script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("/social/users.json")
    .success(function (response) {$scope.users = response.users;});
});
</script>

然后我收到这个答案

{
    "user": [
        {
            "id": 1,
            "email": "email",
            "name": "name",
            "last_name": "last",
            "username": "itsme",
            "password":"password",
            "created": "2015-09-21T16:44:47+0000",
            "modified": "2015-09-21T16:52:08+0000"
        }
    ]
}

如何解析重复此代码并在 html 中填充数据?

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

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

</div>

【问题讨论】:

标签: php json restful-architecture


【解决方案1】:

两件事:首先,您不需要在 beforeSend 中指定内容类型。 $.ajax 有一个可以使用的特定 contentType 属性,默认情况下它已经是“application/x-www-form-urlencoded; charset=UTF-8”。

其次,使用 $.ajax 的 dataType 属性。您可以使用dataType: 'json',然后您在 success 函数中获得的 response 将已经是 json 解析对象 - 或者只是一个 js 对象-。

您可以从那里访问所有属性、数组等,就像您使用任何其他普通的 javascript 对象一样,从这个 json 解析的响应中访问。

【讨论】:

  • 嗯,你应该知道如何解析一个js数组,或者自己学习。您也可以在 google 中查找如何直接使用 JQuery 或 Javascript 填充 HTML。
猜你喜欢
  • 2021-12-26
  • 2010-12-31
  • 2020-06-14
  • 2013-08-18
  • 1970-01-01
  • 2014-05-19
  • 2013-11-15
  • 1970-01-01
相关资源
最近更新 更多