使用编码的GET请求获取错误

时间:2015-11-20 13:27:32

标签: javascript java spring url-encoding datatables-1.10

我正在尝试进行GET调用以重新加载数据表。

我正在对以下日期进行编码,并将 2015/11/01 18:38 2015/11/09 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"

但是,如果我使用以下网址,它可以正常工作:

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

因此,当我将它们作为查询字符串传递时,编码日期会起作用,但当我将它们作为路径变量传递时会失败。

  

我在Spring Controller中的Java代码是:

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

我的javascript电话:

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
  

我在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) {

1 个答案:

答案 0 :(得分:0)

我不知道确切的问题,因为它通过解码值看起来很好。但作为开发人员,我认为这不是在URL中发送DATETIME值的专业方法。您必须以Timestamp等简单方式发送它们,然后将它们在业务逻辑层中重新映射为实际的Datetime对象。