2015-04-17 34 views
0

基本上,我有一个HTML搜索表单,它允许我在数据库中进行搜索。当提交表单时会调用JavaScript函数,但我没有重定向到所需的页面。 “请求方法”POST'不支持“是收到的错误消息。不支持请求方法'POST'Spring Boot

我的代码:

<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

什么是你在浏览器的网络流量选项卡上看到的网址? – kryger

+0

@kryger GET http:// localhost:8080/showForm/ss/filter/deviceName POST http:// localhost:8080/ – user2246955

+0

'window.location.href'发出一个GET,所以目前还不清楚为什么你期望这个成为一个POST。无论采用哪种方式,这都是一种颇为人为和非正统的提交表单的方式。 – kryger

回答

0

您不必执行window.location.href后在JavaScript返回假 - 所以我怀疑后的JavaScript执行异步GET请求窗口.location.href,那么函数结束并且控制权被传回给表单,它只是执行普通的POST动作,但是你还没有定义一个动作URL(它解释了GET,然后你说你在POST中看到过的POST请求网络选项卡)。

除此之外,作为在评论中提到的,你可能不应该使用POST一个搜索表单 - 看一看http://www.w3schools.com/tags/ref_httpmethods.asp

相关问题