2015-11-20 75 views
0

我正在嘗試執行GET調用來重新加載數據表。編碼GET請求獲取錯誤

我編碼以下的日期和把他們2015年11月1日18:382015年11月9日18:38爲路徑變量。

2015/11/01 18:38 -> encoded -> 2015%2F11%2F01%2018%3A38 
2015/11/09 18:38 -> encoded -> 2015%2F11%2F09%2018%3A38 

我收到錯誤:

http://localhost:8880/myapp/getAlertFilterHistory/2015%2F11%2F01%2018%3A38/2015%2F11%2F09%2018%3A38/?_=1448024862539 
400 Bad Request 76ms jquery.min.js (line 4) 
"NetworkError: 400 Bad Request - http://localhost:8880/myapp/getAlertFilterHistory/2015%2F11%2F01%2018%3A38/2015%2F11%2F09%2018%3A38/?_=1448024862539" 

但是,如果使用下面的URL,它工作正常:

http://localhost:8880/myapp/getAlertFilterHistory/null/null/?_=1448024656575 

此外,當我經過編碼的日期作爲查詢字符串,它作品。

http://localhost:8880/myapp/validateFilterHistoryDates?filterStartDate=2015%252F11%252F01%252018%253A38&filterEndDate=2015%252F11%252F09%252018%253A38 

所以編碼日期工作時,我將它們作爲查詢字符串傳遞,但失敗時,我將它們作爲路徑變量傳遞。

My Java code in Spring Controller is:

@RequestMapping("getAlertFilterHistory/{fromDateStr}/{toDateStr}") 
    @ResponseBody 
    public String getAlertFilterHistory(@PathVariable String fromDateStr, @PathVariable String toDateStr, HttpSession session) { 
} 

My javascript call:

var filterStartDate = encodeURIComponent($('#filterStartDate').val()); 
var filterEndDate = encodeURIComponent($('#filterEndDate').val()); 

var filterHistortTable = $('#filterConfigurationsTable').DataTable(); 
//Clear the datatable 
filterHistortTable.clear().draw(); 
filterHistortTable.ajax.url('getAlertFilterHistory/'+filterStartDate+'/'+filterEndDate+'/').load(); 
//filterHistortTable.ajax.url('getAlertFilterHistory/null/null/').load(); // this works 

My datatable code in javascript

var filterConfigurationsDataTable = $('#filterConfigurationsTable').dataTable({ 
        "sAjaxSource" : 'getAlertFilterHistory/null/null/',     
        "sPaginationType": "bootstrap", 
        "order": [[ 0, "desc" ]], 
        "bAutoWidth": false, 
        "bRetrieve": true, 
        "bProcessing": true,           
        "aoColumns": [ 
         { "data": "requestDate" }, 
         { "data": "requestType" }, 
         { "data": "applicationNames" }, 
         { "data": "hosts" }, 
         { "data": "matchStrings" }, 
         { "data": "regEx" }, 
         { "data": "activationTime" }, 
         { "data": "expirationTime" }, 
         { "data": "modifiedBy" }, 
         { "data": "ticket" }, 
         { "data": "api"}, 
         { "data": "description"} 

        ] ,     
        "scrollX": true 
       }); 

有誰知道爲什麼發生這種情況?

編輯

我仍然不知道爲什麼它正在發生,我改變了我的代碼來接受查詢字符串,而不是URI路徑和它的工作。

我在JSP改變:

//filterHistortTable.ajax.url('getAlertFilterHistory/'+filterStartDate+'/'+filterEndDate+'/').load(); 
filterHistortTable.ajax.url('getAlertFilterHistory?fromDateStr='+filterStartDate+'&toDateStr='+filterEndDate).load(); 

,並在我的控制器:

@RequestMapping(value = "/getAlertFilterHistory", method = RequestMethod.GET) 
    @ResponseBody 
    public String getAlertFilterHistory(@RequestParam(value = "fromDateStr", required = false) String fromDateStr, 
      @RequestParam(value = "toDateStr", required = false) String toDateStr, HttpSession session) { 
    //(@PathVariable String fromDateStr, @PathVariable String toDateStr, HttpSession session) { 
+0

將過濾放入路徑是一種不好的做法。使用參數。 Pat的資源 – VDanyliuk

+0

我可以使用post請求,它不需要編碼。但我想做一個Get,因爲我只是提取數據來顯示。 – Zeeshan

+0

如何使用Ajax或某些腳本機制將請求發送到服務器?嘗試在請求中將'Content-type'設置爲'application/x-www-form-urlencoded; charset = utf-8' .. – hagrawal

回答

0

我不知道確切的問題,因爲它看起來的價值觀解碼罰款。但作爲開發人員,我認爲這不是在URL中發送DATETIME值的專業方式。您必須以時間戳等簡單的方式發送它們,然後將它們重新映射到實際的Datetime對象中。