基本上,我有一个HTML搜索表单,允许我在数据库中搜索。提交表单时会调用JavaScript函数,但我没有重定向到所需的页面。 “请求方法'POST'不受支持”是收到的错误消息。
我的代码:
<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";
}
答案 0 :(得分:0)
执行window.location.href
后,你的javascript中没有返回false - 所以我怀疑在javascript执行对window.location.href的异步GET请求之后,函数结束并且控制权被传递回到表单,它只执行正常的POST操作,但是你还没有定义一个动作URL(这解释了GET然后你说你在网络选项卡中看过的POST请求)。
除此之外,正如评论中所提到的,您可能不应该在搜索表单中使用POST - 请查看http://www.w3schools.com/tags/ref_httpmethods.asp