我有簡單的代碼,通過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」 –