[英]How to load data in ng table from REST web service that return a JSON
I want to load my table from a JSON that returns my REST web services in SpringMVC, but I get the following error that says that my rest method doesn't support the GET method. 我想从一个在SpringMVC中返回我的REST Web服务的JSON加载我的表,但是我得到以下错误,说我的rest方法不支持GET方法。
The URL that my controller have mapped is this one 我的控制器映射的URL就是这个
http://localhost:8080/MyApp/doc/showDocTable/
and the URL changes from that to this 并且URL从此更改为此
http://localhost:8080/MyApp/doc/showDocTable/?count=10&page=1
I don't know why the URL changes to that since I'm doing just the basic example from the website http://ng-table.com/#/loading/demo-external-array 我不知道为什么网址会改变,因为我只是从网站http://ng-table.com/#/loading/demo-external-array做基本的例子
Here is the function in my controller that calls the webservices 这是我的控制器中调用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;
});
}
});
And here is my controller method in Spring MVC 这是我在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);
}
And here is the error that I get in my browser 这是我在浏览器中收到的错误
GET XHR http://localhost:8080/MyApp/doc/showDocTable/?count=10&page=1 [HTTP/1.1 405 Request method 'GET' not supported 6ms]
and here the error in my console 这是我的控制台中的错误
WARN 2016-08-11 12:46:58,206 [http-listener-1(4)] org.springframework.web.servlet.PageNotFound - Request method 'GET' not supported
I think the problem is that somehow ngTable changes the URL to that weird URL and my web method in Spring doesn't know what to do. 我认为问题在于某种方式ngTable将URL更改为那个奇怪的URL,我在Spring中的Web方法不知道该怎么做。
EDIT 1 I removed the "/" from my backend method url like this `@RequestMapping(value = "/doc/showDocTable", method = RequestMethod.GET) public ResponseEntity> showTable() { List listHistDoc = docDAO.getDocs(); 编辑1我从我的后端方法url中删除了“/”,如“@RequestMapping(value =”/ doc / showDocTable“,method = RequestMethod.GET)public ResponseEntity> showTable(){List listHistDoc = docDAO.getDocs();
return new ResponseEntity<List<HistDoc>>(listaHistDoc, HttpStatus.OK);
}`
and also I removed the "/" from the getData method in angularJs controller 我还从angularJs控制器中的getData方法中删除了“/”
var Api = $resource('http://localhost:8080/MyApp/doc/showDocTable');
But now I get this error in my browser firefox console 但现在我在我的浏览器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)
EDIT 2: Here is my HTML 编辑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>
and here is the JSON that I return from the backend 这是我从后端返回的JSON
[
{
"idHisReg":null,
"idDoc":1,
"name":doc one,
"description:" "a description"
},
{
"idHisReg":null,
"idDoc":2,
"name": doc two,
"description:" "a seconddescription"
}
]
Use Api.query(...)
instead of using Api.get()
should solve your error: 使用Api.query(...)
代替使用Api.get()
可以解决您的错误:
Error in resource configuration for action
get
. 动作get
资源配置出错。 Expected response to contain an object but got an array (Request: GET http://localhost:8080/MyApp/doc/showDocTable ) 预期响应包含一个对象但得到一个数组(请求:GET http:// localhost:8080 / MyApp / doc / showDocTable )
您必须使用$resource.query()
( Api.query()
)而不是$resource.get()
( Api.get()
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.