【问题标题】:How to load data in ng table from REST web service that return a JSON如何从返回 JSON 的 REST Web 服务加载 ng 表中的数据
【发布时间】:2016-08-11 17:27:17
【问题描述】:

我想从在 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 的基本示例

这是我的控制器中调用网络服务的函数

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,而我在 Spring 中的 web 方法不知道该怎么做。

编辑 1 我从我的后端方法 url 中删除了“/”,就像这样 `@RequestMapping(value = "/doc/showDocTable", method = RequestMethod.GET) 公共响应实体> showTable() { 列表 listHistDoc = 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"
   }
]

【问题讨论】:

  • 这不是URL的问题,因为它只是添加了查询参数。您可以使用 postman 或 Advanced Rest Client(Chrome 插件)等应用程序测试相同的 URL 吗?您在开发者工具中看到 json 响应了吗?
  • URL 很好,因为我已经对该方法进行了单元测试,如果我访问该 URL (localhost:8080/MyApp/doc/showDocTable),我得到的 JSON 响应很好,我用 firebug 测试了它,我也已经使用了该 URL 但使用另一个表插件(角度数据表),我能够使用该 URL 加载表,但我现在想使用 ngTable,但我得到了那个错误
  • 你能在后端调试/记录请求的 url 吗?我认为我对查询参数的看法是错误的,因为它们可能是问题所在。但我相信您的后端不支持查询参数,因此无法识别端点。尝试查看是否调用了正确的方法。如果没有,则尝试将参数添加到 $resource(url, { page: '@page', count: '@count' })
  • 你指的是什么网址?带有参数的那个,因为当我尝试访问此 url http://localhost:8080/MyApp/doc/showDocTable/?count=10&amp;page=1 带有参数的那个时,我在后端的控制台中收到此错误WARN 2016-08-11 12:46:58,206 [http-listener-1(4)] org.springframework.web.servlet.PageNotFound - Request method 'GET' not supported
  • 对不起,是的,我确实提到了那个。如果删除尾部斜杠怎么办?像这样:http://localhost:8080/MyApp/doc/showDocTable?count=10&amp;page=1后端可以用吗?

标签: javascript angularjs spring-mvc angular-resource ngtable


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 2012-07-11
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多