ajax日期參数格式问题

今天遇到ajax传输日期參数后台无法识别的问题,错误异常例如以下。

从异常中能够看出传输到后台的日期数据格式为Thu Aug 13 2015 19:45:20 GMT+0800 (中国标准时间),这样的格式的日期数据格式服务端无法解析。

Caused by: java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "Mon Aug 17 2015 12:00:40 GMT+0800 (中国标准时间)"
    at org.springframework.beans.propertyeditors.CustomDateEditor.setAsText(CustomDateEditor.java:111) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:449) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:422) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:195) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    ... 39 common frames omitted



浏览器端的ajax请求

$.ajax({
    url: './test/ajax.do',
    data: {
        start: new Date(),
        end: new Date()
    },
    dataType: 'json',
    type: 'post'
}).done(function(json){
    console.dir(json);
});



浏览器提交的日期数据格式
日期格式



从图片上能够看到日期參数在提交的时候。已经用JavaScript默认的toString()方法转为字符串格式。
那么,ajax怎样传输日期格式数据或者其它复杂类型数据?要解决问题就必须了解ajax支持传输什么类型的数据。

事实上ajax发送请求參数和接收server端返回的数据都是文本数据,ajax不支持二进制传输数据。所以ajax在传输參数的时候,会调用toString方法把參数转成字符串。

ajax支持post和get方式请求,get方式的请求參数通过url来传输,因为浏览器对url的长度有限制(通常不超过2048字节)。所以get请求參数不能过大。

post请求使用POST方式提交(与Form的POST方式提交一致)。没有数据限制大小。ajax的post和get的数据都是以文本方式传输,不管是client提交的数据还是服务端返回的数据。
日期一般由年、月、日、小时、分、秒、毫秒组成,能够把日期转为2015-08-17 10:12:14的格式。也能够转为从1970年1月1日0时到如今的毫秒数格式,如1439782850609,仅仅要在服务端做对应的日期格式转换就可以。

日期格式(年-月-日 时:分:秒)

//DateUtils请看博客http://blog.csdn.net/accountwcx/article/details/47446225
$.ajax({
    url: './test/ajax.do',
    data: {
        start: DateUtils.format(new Date(), 'yyyy-MM-dd HH:mm:ss'),
        end: DateUtils.format(new Date(), 'yyyy-MM-dd HH:mm:ss')
    },
    dataType: 'json',
    type: 'post'
}).done(function(json){
    console.dir(json);
});

浏览器提交的日期数据格式
日期数据

服务端处理日期(SpringMVC)

@Controller
@RequestMapping("/test")
public class TestController {

    @InitBinder 
    public void initBinder(WebDataBinder binder){
        //日期处理
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
    }

    /**
     * 日期
     * @param start 開始日期
     * @param end 结束日期
     * @param response
     */
    @RequestMapping("/ajax.do")
    public void ajax(Date start, Date end, HttpServletResponse response){
        response.setContentType("text/plain;charset=utf-8");
        response.setCharacterEncoding("utf-8");

        Map<String, Object> json = new HashMap<String, Object>();
        json.put("start", start);
        json.put("end", end);

        try{
            //把日期返回去
            response.getWriter().write(JSON.toJSONString(json));
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}


日期格式(1970年1月1日到如今的毫秒数)

$.ajax({
    url: './test/ajax.do',
    data: {
        start: new Date().getTime(),
        end: new Date().getTime()
    },
    dataType: 'json',
    type: 'post'
}).done(function(json){
    console.dir(json);
});


浏览器提交的日期数据格式
日期格式

服务端处理日期(SpringMVC)

@Controller
@RequestMapping("/test")
public class TestController {   
    /**
     * 日期
     * @param start 開始日期
     * @param end 结束日期
     * @param response
     */
    @RequestMapping("/ajax.do")
    public void ajax(Long start, Long end, HttpServletResponse response){
        response.setContentType("text/plain;charset=utf-8");
        response.setCharacterEncoding("utf-8");

        Date startDate = new Date(start);
        Date endDate = new Date(end);

        Map<String, Object> json = new HashMap<String, Object>();
        json.put("start", startDate);
        json.put("end", endDate);

        try{
            //把日期返回去
            response.getWriter().write(JSON.toJSONString(json));
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
原文地址:https://www.cnblogs.com/liguangsunls/p/7054714.html