如何从返回JSON的REST web服务加载ng表中的数据
How to load data in ng table from REST web service that return a JSON
我想从返回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 'GET' 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)
- ng绑定和ng href问题.ng href未从控制器加载数据
- 用于在ng映射中加载GeoJSON的回调
- 有没有办法在AngularJS中强制重新加载递归ng-include
- 避免在用ng href加载样式表之前显示内容
- 隐藏元素直到加载完成(ng斗篷不是我需要的)
- 当在ng视图中加载新路由时,Tabbing(tabindex)针对错误的元素
- Angular 和 django 无法加载 ng-bind
- 在 DOM 中动态重新加载 ng 重复数据
- 在AngularJs中无法加载ng-view
- Angular 不会加载 ng-admin
- 在加载ng选项之前更新模型时选择选项
- 重新加载ng重复,使用新数据
- 如何在AngularJS中重新加载ng include
- Ionic框架未加载ng模型变量
- 重新加载ng表不起作用
- Angular/Javascript -重新加载ng-repeat的数据后更改类
- 如何加载ng-src取决于值
- 在加载ng-view模板失败时显示错误页面
- 如何从返回JSON的REST web服务加载ng表中的数据
- 未捕获的错误:即使在加载ng路线后也有角度