【问题标题】:how to print the JSON-RPC output which is in JSON format in a html-table format如何以 html-table 格式打印 JSON 格式的 JSON-RPC 输出
【发布时间】:2020-06-15 18:43:55
【问题描述】:

基本上,我有一个包含客户端和服务器的设置。 客户端在angularjs中实现,它将get请求发送到python中的服务器,服务器连接到mongoDB并检索结果并以json格式发回响应。

我发送的请求如下:

const data = {
  "jsonrpc" : "2.0",
  "method" : "faultall",
  "id" : "1",

  "params": {'argument' : 'something' }
}
this.http.post('http://localhost:80/jsonrpc',data).subscribe( res => console.log(this.json =res.text()))

虽然我得到的回应是:

{"result": [["water", "001"], ["water", "002"], ["temp", "003"]], "id": "1", "jsonrpc": "2.0"}

现在我希望将响应打印为我的 html 页面中的表格,如图所示:

【问题讨论】:

    标签: json angularjs html-table get json-rpc


    【解决方案1】:

    也许,这段代码会有用

    class AppController {
      
      constructor($q) {
        this.deferred = $q.defer();
      }
      
      getData() {
        const response = {"result": [["water", "001"], ["water", "002"], ["temp", "003"]], "id": "1", "jsonrpc": "2.0"};
        setTimeout(() => {
          this.deferred.resolve(response.result);
        }, 1000);
        return this.deferred.promise;
      }
      
      fillTable() {
        this.dataLoading = true;
        this.getData()
        .then(data => {
          this.data = data;
          this.dataLoading = false;
        });
      }
    }
    
    angular.module('app', [])
    .controller('appController', AppController);
    
    AppController.$inject = ['$q'];
    
    angular.bootstrap(
      document.getElementById('root'),
      ['app']
    );
    html, body {
      height: 100%;
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    
    <div id="root" ng-controller="appController as ctrl">
      <div style="margin: 1rem;">
        <button 
          class="btn btn-primary"
          ng-click="ctrl.fillTable()"
        >
          <span ng-bind="ctrl.dataLoading ? 'Loading' : 'Fetch data'"></span>
        </button>
      </div>
      <table class="table" ng-show="ctrl.data.length > 0">
        <thead>
          <tr>
            <th>Name</th>
            <th>Id</th>
          </tr>
        </thead>
          <tr ng-repeat="row in ctrl.data">
            <td ng-bind="::row[0]"></td>
            <td ng-bind="::row[1]"></td>
          </tr>
        <tbody>
        </tbody>
      </table>
      <pre>{{ $ctrl.data | json}}</pre>
    </div>

    【讨论】:

    • 嗨 @eustatos 我应该将它创建为外部 js 文件吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 2021-11-28
    • 2019-02-21
    • 2016-10-08
    相关资源
    最近更新 更多