HTTP 状态 405 - 不支持请求方法“POST”(Spring MVC)
HTTP Status 405 - Request method 'POST' not supported (Spring MVC)
>我收到此错误:HTTP Status 405 - Request method 'POST' not supported
我正在尝试做的是制作一个带有下拉框的表单,该下拉框根据在另一个下拉框中选择的其他值进行填充。例如,当我在customerName
框中选择一个名称时,应运行.jsp页面中的onChange
函数,然后再次加载customerCountry
框中的相应值。
但是我收到此HTTP状态405错误。我已经在互联网上搜索了解决方案,但找不到任何有帮助的东西。以下是我的代码的相关部分:
JSP页面的一部分
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
.error { color: red; }
</style>
<script>
function repopulate(){
document.deliveryForm.submit();
}
function setFalse(){
document.getElementById("hasId").value ="false";
document.deliveryForm.submit();
// document.submitForm.submit(); (This was causing the error)
}
</script>
</head>
<body>
<h1>Create New Delivery</h1>
<c:url var="saveUrl" value="/test/delivery/add" />
<form:form modelAttribute="deliveryDtoAttribute" method="POST" action="${saveUrl}" name="deliveryForm">
<table>
<tr>
<td><form:hidden id="hasId" path="hasCustomerName" value="true"/></td>
</tr>
<tr>
<td>Customer Name</td>
<td><form:select path="customerName" onChange="repopulate()">
<form:option value="" label="--- Select ---" />
<form:options items="${customerNameList}" />
</form:select>
</td>
<td><form:errors path="customerName" cssClass="error" /></td>
</tr>
<tr>
<td>Customer Country</td>
<td><form:select path="customerCountry">
<form:option value="" label="--- Select ---" />
<form:options items="${customerCountryList}" />
</form:select>
</td>
<td><form:errors path="customerCountry" cssClass="error" /></td>
</tr>
</form:form>
<form:form name="submitForm">
<input type="button" value="Save" onClick="setFalse()"/>
</form:form>
</body>
</html>
控制器的一部分:
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String getDelivery(ModelMap model) {
DeliveryDto deliveryDto = new DeliveryDto();
model.addAttribute("deliveryDtoAttribute", deliveryDto);
model.addAttribute("customerNameList",
customerService.listAllCustomerNames());
model.addAttribute("customerCountryList", customerService
.listAllCustomerCountries(deliveryDto.getCustomerName()));
return "new-delivery";
}
// I want to enter this method if hasId=true which means that a value in the CustomerName
// drop down list was selected. This should set the CountryList to the corresponding values
// from the database. I want this post method to be triggered by the onChange in the jsp page
@RequestMapping(value = "/add", method = RequestMethod.POST, params="hasCustomerName=true")
public String postDelivery(
@ModelAttribute("deliveryDtoAttribute") DeliveryDto deliveryDto,
BindingResult result, ModelMap model) {
model.addAttribute("deliveryDtoAttribute", deliveryDto);
model.addAttribute("customerNameList",
customerService.listAllCustomerNames());
model.addAttribute("customerCountryList", customerService
.listAllCustomerCountries(deliveryDto.getCustomerName()));
return "new-delivery";
}
// This next post method should only be entered if the save button is hit in the jsp page
@RequestMapping(value = "/add", method = RequestMethod.POST, params="hasCustomerName=false")
public String postDelivery2(
@ModelAttribute("deliveryDtoAttribute") @Valid DeliveryDto deliveryDto,
BindingResult result, ModelMap model) {
if (result.hasErrors()) {
model.addAttribute("deliveryDtoAttribute", deliveryDto);
model.addAttribute("customerNameList",
customerService.listAllCustomerNames());
model.addAttribute("customerCountryList", customerService
.listAllCustomerCountries(deliveryDto.getCustomerName()));
return "new-delivery";
} else {
Delivery delivery = new Delivery();
//Setters to set delivery values
return "redirect:/mis/home";
}
}
为什么会出现此错误?任何帮助将不胜感激!谢谢
编辑:将hasId
更改为hasCustomerName
。不过,我仍然收到HTTP Status 405 - Request method 'POST' not supported
错误。
EDIT2:注释掉setFalse
函数中导致错误的行
//D
我不确定这是否有帮助,但我遇到了同样的问题。
您正在使用具有CSRF保护的springSecurityFilterChain。这意味着当您通过 POST 请求发送表单时,您必须发送令牌。尝试将下一个输入添加到表单中:
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
检查您返回的是@ResponseBody还是@ResponseStatus
我也有类似的问题。我的控制器看起来像这样:
@RequestMapping(value="/user", method = RequestMethod.POST)
public String updateUser(@RequestBody User user){
return userService.updateUser(user).getId();
}
使用 POST 请求调用时,我总是收到以下错误:
HTTP 状态 405 - 不支持请求方法"POST"
过了一会儿,我发现该方法实际上是被调用的,但是由于没有@ResponseBody也没有@ResponseStatus Spring MVC引发了错误。
要解决此问题,只需添加一个@ResponseBody
@RequestMapping(value="/user", method = RequestMethod.POST)
public @ResponseBody String updateUser(@RequestBody User user){
return userService.updateUser(user).getId();
}
或对您的方法@ResponseStatus。
@RequestMapping(value="/user", method = RequestMethod.POST)
@ResponseStatus(value=HttpStatus.OK)
public String updateUser(@RequestBody User user){
return userService.updateUser(user).getId();
}
您可能需要更改该行
@RequestMapping(value = "/add", method = RequestMethod.GET)
自
@RequestMapping(value = "/add", method = {RequestMethod.GET,RequestMethod.POST})
问题是你的控制器需要一个参数hasId=false或hasId=true,但你没有传递它。您的隐藏字段具有 id hasId,但作为 hasCustomerName 传递,因此没有映射匹配。
将隐藏字段的路径更改为 hasId,或将映射参数更改为期望 hasCustomerName=true 或 hasCustomerName=false。
我发现了导致HTTP错误的问题。
在由"保存"按钮触发的setFalse()
函数中,我的代码正在尝试提交包含该按钮的表单。
function setFalse(){
document.getElementById("hasId").value ="false";
document.deliveryForm.submit();
document.submitForm.submit();
当我删除document.submitForm.submit();
时,它可以工作:
function setFalse(){
document.getElementById("hasId").value ="false";
document.deliveryForm.submit()
@Roger Lindsjö 感谢您发现我没有传递正确参数的错误!
在我的情况下,网址以/结尾
付款网址(旧)=/获取详细信息/
我刚刚删除了尾随/
付款网址(新)=/获取详细信息
它奏效了
由于其他原因,我遇到了类似的问题(test-response
csrf 令牌中未添加网址模式)我通过在以下属性中允许我的 URL 模式来解决config/local.properties
:
csrf.allowed.url.patterns = /[^/]+(/[^?])+(sop-response)$,/[^/]+(/[^?])+(merchant_callback)$,/[^/]+(/[^?])+(hop-response)$
修改为
csrf.allowed.url.patterns = /[^/]+(/[^?])+(sop-response)$,/[^/]+(/[^?])+(merchant_callback)$,/[^/]+(/[^?])+(hop-response)$,/[^/]+(/[^?])+(test-response)$
- 如何使用WCF服务和javascript表单post上传.doc文件
- Jquery POST未填充数组
- 如何在php文件中获取$.post-ajax传递的值
- Angularjs Post未向Spring JWT发送标头
- 是否可以将javascript json ajax POST的多个参数传递给Spring MVC控制器
- HTTP 状态 405 - 不支持请求方法“POST”(Spring MVC)
- 400 错误请求错误在 Ajax POST 到 spring RestController
- Spring MVC 控制器不会拦截 Ajax POST
- 使用 AngularJS 登录 Spring Boot $http.post
- Spring MVC AngularJS - Post Redirect
- 使用AngularJS和Spring Boot的Post基本认证语义
- AngularJs表单post数据给null值在我的spring控制器
- Spring-MVC - JSON ajax post到控制器错误
- Spring和ExtJS &;400个错误请求&;使用PUT,但不使用POST
- 在node.js中上传文件,在spring boot中POST multipart
- Spring和ExtJS“错误请求”与POST方法
- 在SPRING MVC中使用JSON进行POST请求的正确方法
- Spring-405(不支持请求方法'POST')
- AJAX POST丢失了对模型属性的引用-Spring
- 如何使用 Spring MVC 在 jQuery Post 函数中配置 HTTP 头