![](/img/trans.png)
[英]How to populate table in Angular with JSON data from a web Service?
[英]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
我不知道為什么網址會改變,因為我只是從網站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,我在Spring中的Web方法不知道該怎么做。
編輯1我從我的后端方法url中刪除了“/”,如“@RequestMapping(value =”/ doc / showDocTable“,method = RequestMethod.GET)public ResponseEntity> showTable(){List 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"
}
]
使用Api.query(...)
代替使用Api.get()
可以解決您的錯誤:
動作
get
資源配置出錯。 預期響應包含一個對象但得到一個數組(請求:GET http:// localhost:8080 / MyApp / doc / showDocTable )
您必須使用$resource.query()
( Api.query()
)而不是$resource.get()
( Api.get()
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.