我有简单的代码,通过AngularJS将表单数据传递给弹簧控制器。在Chrome调试器中,我可以看到正确的值正在传递请求,但是我收到一个错误,说405 (Request method 'POST' not supported)
,并且我的控制器中的调试指针没有命中。所以我明白这是因为我的POST请求没有映射到我的控制器,但我已经做了一切正确的事情。Spring - 405(请求方法' POST '不支持)
形式: -
<form name="empForm" ng-controller="insertEmpCtrl" ng-submit="insertEmp()">
<table>
<tr><td>First name: <input type="text" class="form-control" name="fname" ng-model="formData.fname"/></td></tr>
<tr><td>Last name: <input type="text" class="form-control" name="lname" ng-model="formData.lname"/></td></tr>
<tr><td>Contact no: <input type="text" class="form-control" name="contact" ng-model="formData.contactno"/></td></tr>
<tr>
<td><input type="submit" value="Save" /> <input type="reset" value="Edit"></td>
<td></td>
</tr>
</table>
AngularJS控制器: -
empApp.controller('insertEmpCtrl',function($scope,$http){
$scope.insertEmp = function(){
$http.post("http://localhost:8080/IdeaOne#/addemp", $scope.formData, {
withCredentials: true,
headers: {'Content-Type': 'application/json'},
transformRequest: angular.identity
}).success(function(){console.log("done")}).error(function(){console.log("error")});
};
});
弹簧控制器: -
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String home(ModelMap model){
return "hello";
}
//method redirects to hello.jsp
@RequestMapping(value="/addemp",method = RequestMethod.POST)
public String addEmployee(@RequestBody Employee employee) {
EmployeeManager.empDB.add(employee);
return "hello";
}
//method returns a link list as a json doc to the front end
@RequestMapping(value="/viewall",method = RequestMethod.GET)
public @ResponseBody
LinkedList<Employee> viewAll(ModelMap model){
return EmployeeManager.viewEmployees();
}
}
有人可以帮我弄清楚我哪里出错了吗?
1.您是否在控制器中包含'@ Controller'? 2.为什么不在你的'form'中使用'action'? – jhyap
请检查铬的控制台,看看是什么URL被击中 – geoand
值=“/ addemp”应该是值=“IdeaOne#/ addemp” –