不在Jquery数据表中显示JSON数据

时间:2017-10-27 17:42:49

标签: java jquery jsp spring-mvc datatables

我正在尝试将Java控制器中的示例数据显示到表中,但是,它不呈现数据也不显示任何错误消息。暂时,我试图以JSON格式显示简单的模拟数据......但是,该表不显示任何数据或任何错误消息..

我正在使用JSP,AngualrJS和jquery作为前端,Java作为后端..

这是我的JSP页面表:

<table class="table table-striped table-bordered dt-responsive nowrap" id="myTable">
<thead>
<tr>
<th>ID</th>
<th>Status</th>
</tr>
</thead>
</table

这是我的JSP页面:

$.ajax({
url:"/home/review/",
success:function(data) {
var table = $('#myTable').DataTable( {
"data": data,
dom: "Bfrtip",
"iDisplayLength": 15,
"lengthChange": false,
"ordering": true,
"info": false,
'columnDefs': [
{
'targets': 1,
'orderable': false,
'checkboxes': {
'selectRow': true
}
}
],
'select': {
'style': 'multi'
},

    { "data": "id", "defaultContent": "" },
    { "data": "status", "defaultContent": ""}
],
});
}
});

这是我的angularJS控制器:

angular.module('myApp').controller('HomeController', ['DTOptionsBuilder', 'DTColumnBuilder','HomeService', '$scope', function(DTOptionsBuilder, DTColumnBuilder, HomeService, $scope) {
var test_URL = '/home/review/';
var self = $scope;
var vm = this;

vm.dtOptions = HomeService.fetchList();
console.log(vm.dtOptions);
}]);

我的angularJS服务:

angular.module('myApp').factory(HomeService', ['$http', '$q', function($http, $q){

var URL= '/home/review/';

var factory = {
        fetchList: fetchList
};

return factory;

function fetchList() {
    var deferred = $q.defer();
    $http.get(URL)
        .then(
        function (response) {
            deferred.resolve(response.data);
        },
        function(errResponse){
            console.error('Error while fetching List');
            deferred.reject(errResponse);
        }
    );
    return deferred.promise;
}
}]);

这是我的Java后端控制器:

@RequestMapping(value = "/review/", method = RequestMethod.GET)
public String geList() throws JSONException {
logger.info("List All Controller");
System.out.println("Fetching All list...");

    JSONArray resultarray = new JSONArray();
    String resultString = "";
            JSONObject result = new JSONObject();
            result.put("id","1234");
            result.put("status", "New");
            resultarray.put(result);

        resultString = resultarray.toString(4);
        System.out.println(resultString);

    return resultString;
}

1 个答案:

答案 0 :(得分:0)

尝试使用JSON.parse()传递数据,同时传递给DataTable。