ajax 丢失 传值 日期_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);

});

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

b1b9d1723e253d979a5d956d4355dd02.png

从图片上能够看到日期參数在提交的时候。已经用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);

});

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

3d6b4d4d43ff33464418384cdf4fd80d.png

服务端处理日期(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 json = new HashMap();

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

});

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

a4a84a9ac6fe7f5402f4247b2a1a8dfe.png

服务端处理日期(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 json = new HashMap();

json.put("start", startDate);

json.put("end", endDate);

try{

//把日期返回去

response.getWriter().write(JSON.toJSONString(json));

}catch(IOException e){

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值