如何从返回JSON的REST web服务加载ng表中的数据

How to load data in ng table from REST web service that return a JSON

本文关键字:加载 ng 数据 服务 REST 返回 JSON web      更新时间:2023-09-26

我想从返回SpringMVC中REST web服务的JSON中加载我的表,但是我得到以下错误,说我的REST方法不支持get方法。

控制器映射的URL是这个

http://localhost:8080/MyApp/doc/showDocTable/

URL从那个变成了这个

http://localhost:8080/MyApp/doc/showDocTable/?count=10&page=1

我不知道为什么URL会变成那样,因为我只是在做网站http://ng-table.com/#/loading/demo-external-array

的基本示例这是我的控制器中调用webservices的函数
var Api = $resource('http://localhost:8080/MyApp/doc/showDocTable/');
            this.tableParams = new NgTableParams({}, {
                getData: function (params) {
                    // ajax request to api
                    return Api.get(params.url()).$promise.then(function (data) {
                        params.total(data.inlineCount); // recal. page nav controls
                        return data.results;
                    });
                }
            });

这是我在Spring MVC中的控制器方法

@RequestMapping(value = "/doc/showDocTable/", method = RequestMethod.GET)
public ResponseEntity<List<HistDoc>> showTable() {
    List<HistDoc> listHistDoc = docDAO.getDocs();

    return new ResponseEntity<List<HistDoc>>(listaHistDoc, HttpStatus.OK);
}

这是我在浏览器中得到的错误

GET XHR  http://localhost:8080/MyApp/doc/showDocTable/?count=10&page=1 [HTTP/1.1 405 Request method &#39;GET&#39; not supported 6ms]

这里是控制台的错误

WARN    2016-08-11 12:46:58,206 [http-listener-1(4)] org.springframework.web.servlet.PageNotFound  - Request method 'GET' not supported

我认为问题是,不知何故,ngTable改变URL到那个奇怪的URL和我的web方法在Spring不知道该怎么做。

编辑1 我从我的后端方法url中删除了"/",如下所示:@RequestMapping(value = "/doc/showDocTable", method = RequestMethod.GET)public ResponseEntity> showTable() {列表listthistdoc = docDAO.getDocs();

    return new ResponseEntity<List<HistDoc>>(listaHistDoc, HttpStatus.OK);
}`

我还从angularJs控制器

中的getData方法中删除了"/"
var Api = $resource('http://localhost:8080/MyApp/doc/showDocTable');

但是现在我在浏览器firefox控制台出现了这个错误

Error: $resource:badcfg
Response does not match configured parameter
Error in resource configuration for action `get`. Expected response to contain an object but got an array (Request: GET http://localhost:8080/MyApp/doc/showDocTable)

编辑2:这是我的HTML

<div class="col-lg-12" ng-controller="EstHistDoc as demo">
    <table ng-table="demo.tableParams" class="table table-bordered table-striped table-condensed">
        <tr ng-repeat="row in $data track by row.idDoc">
            <td data-title="'Name'" filter="{name: 'text'}" sortable="'name'">{{row.name}}</td>
            <td data-title="'Description'" filter="{description: 'text'}" sortable="'description'">{{row.description}}</td>
        </tr>
    </table>

这是我从后端返回的JSON
    [
   {
      "idHisReg":null,
      "idDoc":1,
      "name":doc one,
      "description:" "a description"
   },
   {
      "idHisReg":null,
      "idDoc":2,
      "name": doc two,
      "description:" "a seconddescription"
   }
]

您必须使用$resource.query() (Api.query())而不是$resource.get() (Api.get())

使用Api.query(...)而不是使用Api.get()应该可以解决您的错误:

操作get的资源配置错误。预期回应包含一个对象,但得到一个数组(请求:GET)http://localhost: 8080/MyApp/doc/showDocTable)