2015-04-17 99 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

相關問題