我正在尝试使用 $http.post 方法将一些数据从 angularjs 更新到 Spring MVC Controller 。但我收到错误消息“HTTP 状态 405 - 不支持请求方法‘GET’”。我的代码片段如下:
//AngularJS 服务
updateData : function(data) {
return $http.post('updateData', data).then(
function(response){
// success callback
return response;
},
function(response){
// failure callback
return response;
}
);
}
其中数据如下:
数据 = [{ 编号:1, 名称:'富' }, { 编号:2, 名称:'酒吧' },{ 编号:3, 名称:'巴兹' }];
//Spring MVC Controller 代码
@RequestMapping(value="/updateData", method = RequestMethod.POST)
public @ResponseBody String updateData(@RequestBody Company company) throws Exception {
// I have created POJO for company with id and name getter-setter
// logic to update the data in db
//System.out.println(company.getName());
return "success";
}
我的代码有什么错误吗?
最佳答案
你的数据是数组格式data = [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' },{ id: 3, name: 'baz' }];
.但是你的@RequestBody 只有对象。您的 RequestBody 应该是一个列表。那么只有它能接受
所以请更改您的代码 @RequestBody Company company
至 @RequestBody List<Company> company
.然后告诉我
关于angularjs - HTTP 状态 405 - 请求方法 'GET' 不支持来自 Angular 的 $http.post 调用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43626195/