请求方法 'POST' 不支持 Spring 启动
Request method 'POST' not supported Spring Boot
基本上,我有一个 HTML 搜索表单,可以让我在数据库中进行搜索。提交表单时调用 JavaScript 函数,但我没有重定向到所需页面。
"Request method 'POST' not supported" 是收到的错误信息。
我的代码:
<form th:object="${devices}" method="POST" onsubmit="return fireAction()">
<input type="text" id="search" name="search" />
<input type="submit" value="Search"/>
</form>
function fireAction() {
var searchInput = document.getElementById('search').value;
var searchFilter = document.getElementById('deviceAttributes').value;
var checkbox = document.getElementById('lastEntry').checked;
alert(searchInput + " " + searchFilter + " " + checkbox);
if (searchInput == "" || searchInput == null) {
alert("Search field cannot be null.");
return false;
} else if (checkbox) {
window.location.href = '/current/' + searchInput
+ '/filter/' + searchFilter;
} else {
window.location.href = '/showForm/' + searchInput
+ '/filter/' + searchFilter;
}
}
@RequestMapping(value = "/showForm/{keyword}/filter/{searchFilter}", method = RequestMethod.POST)
public String showForm(@PathVariable("keyword") String keyword,
@PathVariable("searchFilter") String searchFilter, Model model) {
Devices devices = new Devices();
devices.setSearch(keyword);
devices.setSearchFilter(searchFilter);
model.addAttribute(
"addDevices",
device.findByDevicesName(devices.getSearch(),
devices.getSearchFilter()));
return "showForm";
}
@RequestMapping(value = "/current/{keyword}/filter/{searchFilter}", method = RequestMethod.POST)
public String currentDevices(@PathVariable("keyword") String keyword,
@PathVariable("searchFilter") String searchFilter, ModelMap model) {
model.addAttribute("devices", new Devices());
Devices devices = new Devices();
devices.setSearch(keyword);
devices.setSearchFilter(searchFilter);
List<Devices> newList = device.allDevices();
ListIterator<Devices> iterator = newList.listIterator();
List<Devices> resultList = new ArrayList<Devices>();
while (iterator.hasNext()) {
Devices device = iterator.next();
if (searchLastEntry(device, keyword, searchFilter)) {
resultList.add(device);
}
}
model.addAttribute("iterator2", resultList);
return "current";
}
在执行 window.location.href
后,您的 javascript 中没有 return false - 所以我怀疑在 javascript 执行异步 GET 请求后window.location.href,然后函数结束,控制权传回表单,表单只执行正常的 POST 操作,但您尚未定义操作 URL(这解释了 GET 然后POST 请求你说你在网络选项卡中看到了)。
此外,如评论中所述,您可能不应该使用 POST 作为搜索表单 - 看看 http://www.w3schools.com/tags/ref_httpmethods.asp
基本上,我有一个 HTML 搜索表单,可以让我在数据库中进行搜索。提交表单时调用 JavaScript 函数,但我没有重定向到所需页面。 "Request method 'POST' not supported" 是收到的错误信息。
我的代码:
<form th:object="${devices}" method="POST" onsubmit="return fireAction()">
<input type="text" id="search" name="search" />
<input type="submit" value="Search"/>
</form>
function fireAction() {
var searchInput = document.getElementById('search').value;
var searchFilter = document.getElementById('deviceAttributes').value;
var checkbox = document.getElementById('lastEntry').checked;
alert(searchInput + " " + searchFilter + " " + checkbox);
if (searchInput == "" || searchInput == null) {
alert("Search field cannot be null.");
return false;
} else if (checkbox) {
window.location.href = '/current/' + searchInput
+ '/filter/' + searchFilter;
} else {
window.location.href = '/showForm/' + searchInput
+ '/filter/' + searchFilter;
}
}
@RequestMapping(value = "/showForm/{keyword}/filter/{searchFilter}", method = RequestMethod.POST)
public String showForm(@PathVariable("keyword") String keyword,
@PathVariable("searchFilter") String searchFilter, Model model) {
Devices devices = new Devices();
devices.setSearch(keyword);
devices.setSearchFilter(searchFilter);
model.addAttribute(
"addDevices",
device.findByDevicesName(devices.getSearch(),
devices.getSearchFilter()));
return "showForm";
}
@RequestMapping(value = "/current/{keyword}/filter/{searchFilter}", method = RequestMethod.POST)
public String currentDevices(@PathVariable("keyword") String keyword,
@PathVariable("searchFilter") String searchFilter, ModelMap model) {
model.addAttribute("devices", new Devices());
Devices devices = new Devices();
devices.setSearch(keyword);
devices.setSearchFilter(searchFilter);
List<Devices> newList = device.allDevices();
ListIterator<Devices> iterator = newList.listIterator();
List<Devices> resultList = new ArrayList<Devices>();
while (iterator.hasNext()) {
Devices device = iterator.next();
if (searchLastEntry(device, keyword, searchFilter)) {
resultList.add(device);
}
}
model.addAttribute("iterator2", resultList);
return "current";
}
在执行 window.location.href
后,您的 javascript 中没有 return false - 所以我怀疑在 javascript 执行异步 GET 请求后window.location.href,然后函数结束,控制权传回表单,表单只执行正常的 POST 操作,但您尚未定义操作 URL(这解释了 GET 然后POST 请求你说你在网络选项卡中看到了)。
此外,如评论中所述,您可能不应该使用 POST 作为搜索表单 - 看看 http://www.w3schools.com/tags/ref_httpmethods.asp