2017-07-03 54 views
1

我一直在試圖證明我的支架應用的JSON響應使用Tomcat服務器這是一個休息的應用程序,我在前端採用了棱角分明,這裏是我的控制器如何在數據表中顯示獲取請求中的數據?

(function(){ 
    'use strict'; 
    angular.module('app') 
     .controller('wfCtrl', wfCtrl); 

    function wfCtrl($scope, $location, $http) { 

      var table=$('#example1').DataTable(
    { 
       "bServerSide": true, 
       "fnServerData": function (sSource, aoData, fnCallback) { 
        var myData = JSON.stringify(aoData); 
        $.ajax({ 
         "dataType": 'json', 
         "contentType" : "application/json;charset=utf-8", 
         "type": "GET", 
         "url": "http://localhost:8080/Spring4/data/lworkflows", 
         "data": null, 
         "success": fnCallback, 
         "error": function() { 
          console.log('have some problem'); 
         } 
        }); 
       },  
"aoColumns": [ 
    { "mData": null }, // for User Detail 
    { "mData": "code" }, 
    { "mData": "label" }, 
    { "mData": null }, 
    { "mData": null }, 
    { "mData": null }, 
    { "mData": null }, 
] 
, 
"order": [[ 1, "desc" ]]}); 

})(); 

請求URL「http://localhost:8080/Spring4/data/lworkflows」的回報這樣

{"WFLIGHT":{"code":"WFLIGHT","label":"Submit the deal"},"WFCOM":{"code":"WFCOM","label":"COM"},"WFPOCTBR":{"code":"WFPOCTBR","label":"Workflow Retail POC VW"},"WFRISK":{"code":"WFRISK","label":"RISQUES"},"WFDECF":{"code":"WFDECF","label":"DECIDEUR"},"WFETUDE":{"code":"WFETUDE","label":"ETUDE"},"WFADV":{"code":"WFADV","label":"ADV"},"TEST1":{"code":"TEST1","label":"Workflow Retail POC VW"},"WFCOM2":{"code":"WFCOM2","label":"ASSCOM"}} 

錯誤我得到一個JSON數據是

Uncaught TypeError: Cannot read property 'length' of undefined

,因爲我的數據表不能讀取請求發送的數據

我該如何解決?

+0

而不是$ HTTP提供從角? – geo

+1

此外,返回的json不是一個數組,而是一個對象 – geo

+0

@geo我也嘗試過使用$ http,但它給出了相同的錯誤 –

回答

0

解決了響應轉換成數組後

var output = wfs.map(function(obj) { 
    return Object.keys(obj).sort().map(function(key) { 
    return obj[key]; 
    }); 

這裏就是你爲什麼要使用$就以創建請求的完整工作代碼

function wfCtrl($scope, $location, $http) { 


     $http({ 
      method: 'GET', 
      url: 'http://10.10.216.201:8080/Spring4/data/lworkflows' 
     }).then(function successCallback(response) { 
      var wfs = response.data; 

     var output = wfs.map(function(obj) { 
    return Object.keys(obj).sort().map(function(key) { 
    return obj[key]; 
    }); 

}); 
console.log(output); 
      var table=$('#example1').DataTable(
    { 
     "data":output, 
    "columns": [ 
    { "className":  'details-control', 
       "orderable":  false, 
       "data":   null, 
       "defaultContent": '' }, 
    null, 
    null, 
    null, 
    null, 
    null, 
    { "orderable": false } 
    ] 
, 
"order": [[ 1, "desc" ]]}); 

     }, function errorCallback(response) { 
      console.log("error"); 
     }); 

} 
相關問題