Im nhận được lỗi này: HTTP Status 405 - Request method 'POST' not supported
HTTP Status 405 - Yêu cầu phương pháp 'POST' không được hỗ trợ (Spring MVC)
Những gì tôi đang cố gắng làm là tạo ra một hình thức với một hộp thả xuống mà có được dân cư dựa trên khác giá trị được chọn trong một hộp thả xuống khác. Ví dụ: khi tôi chọn tên trong hộp customerName
, chức năng onChange
trong trang .jsp sẽ được chạy và trang được gửi sau đó được tải lại với các giá trị tương ứng trong hộp customerCountry
.
tuy nhiên tôi nhận được lỗi trạng thái HTTP 405 này. Tôi đã tìm kiếm trên Internet một giải pháp nhưng không thể tìm thấy bất cứ thứ gì đã giúp. Dưới đây là những phần liên quan của mã của tôi:
phần của trang 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>
một phần của điều khiển:
@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";
}
}
Tại sao tôi nhận được lỗi này? Bất kì sự trợ giúp nào đều được đánh giá cao! Cảm ơn
EDIT: Đã thay đổi hasId
thành hasCustomerName
. Tôi vẫn nhận được lỗi HTTP Status 405 - Request method 'POST' not supported
.
EDIT2: nhận xét ra các dòng trong setFalse
chức năng đã gây ra lỗi
// D
Tại sao bỏ phiếu xuống? Vui lòng cung cấp cho tôi giải thích để tôi có thể đăng câu hỏi hay hơn trong tương lai. Cảm ơn – dlinx90
cho những người gặp sự cố này và truy cập trang này, cũng kiểm tra xem hành động được xác định có đạt đến điểm cuối hiện tại hay không, đó là vấn đề của tôi :) –