2015-11-20 78 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对象中。