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();
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM