Ajax Date Parameter format problem

Source: Internet
Author: User
Tags string format

Today encountered an issue with the AJAX transfer date parameter background unrecognized, the error exception is as follows. From the exception can be seen in the transfer to the background date data format for Thu 19:45:20 gmt+0800 (China Standard Time), the format of the date data format service side can not be resolved.

caused By:java. Lang. IllegalArgumentException: Could not parse date:unparseable date:"Mon-12:00:40 gmt+0800 (China Standard Time)"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: -) ~[spring-beans-4.1. 7. RELEASE. Jar:4.1. 7. RELEASE]    ... theCommon frames omitted



Ajax requests on the browser side

$.ajax({    ‘./test/ajax.do‘,    data: {        newDate(),        newDate()    },    ‘json‘,    ‘post‘}).done(function(json){    console.dir(json);});



Date data format for browser submissions



You can see from the picture that the date parameter was converted to string format with the JavaScript default ToString () method at the time of submission.
So, how does Ajax transfer date format data or other complex type data? To solve this problem, you must understand what type of data the AJAX support transmits. In fact, the AJAX sends the request parameters and the receiving server side returns the data is the text data, the AJAX does not support the binary data transmission, so Ajax in the transmission parameter, the time will call the ToString method to turn the parameter to the string. Ajax supports post and get mode requests, and the GET request parameters are transmitted via URLs, so the GET request parameter cannot be too large because the browser has a limit on the length of the URL (typically no more than 2048 bytes). Post requests are submitted using post (consistent with the form's post submission), with no data size limit. Both the post and get data for Ajax are transmitted in text, whether the data is submitted by the client or the data returned by the server.
The date is usually composed of year, month, day, hour, minute, second, millisecond, can convert the date to 2015-08-17 10:12:14 format, can also be converted from January 1, 1970 0 o'clock to now the number of milliseconds format, such as 1439782850609, Just make the appropriate date format conversion on the server.

Date format (year-month-day time: minutes: seconds)
//DateUtils请看博客http://blog.csdn.net/accountwcx/article/details/47446225$.ajax({    ‘./test/ajax.do‘,    data: {        start: DateUtils.format(newDate‘yyyy-MM-dd HH:mm:ss‘),        end: DateUtils.format(newDate‘yyyy-MM-dd HH:mm:ss‘)    },    ‘json‘,    ‘post‘}).done(function(json){    console.dir(json);});

Date data format for browser submissions


Service-side processing date (SPRINGMVC)

@Controller@RequestMapping("/test") Public  class testcontroller {    @InitBinder      Public void Initbinder(Webdatabinder Binder) {//Date processingSimpleDateFormat DF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Df.setlenient (false); Binder.registercustomeditor (Date.class,NewCustomdateeditor (DF,true)); }/** * Date * @param start Start date * @param end End Date * @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 =NewHashmap<string, object> (); Json.put ("Start", start); Json.put ("End", end);Try{//Return the date toResponse.getwriter (). Write (Json.tojsonstring (JSON)); }Catch(IOException e)        {E.printstacktrace (); }    }}


Date format (January 1, 1970 to present number of milliseconds)
$.ajax({    ‘./test/ajax.do‘,    data: {        newDate().getTime(),        newDate().getTime()    },    ‘json‘,    ‘post‘}).done(function(json){    console.dir(json);});


Date data format for browser submissions


Service-side processing date (SPRINGMVC)

@Controller@RequestMapping("/test") Public  class testcontroller {       /** * Date * @param start Start date * @param end End Date * @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 =NewDate (start); Date endDate =NewDate (end); map<string, object> json =NewHashmap<string, object> (); Json.put ("Start", StartDate); Json.put ("End", endDate);Try{//Return the date toResponse.getwriter (). Write (Json.tojsonstring (JSON)); }Catch(IOException e)        {E.printstacktrace (); }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Ajax Date Parameter format problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.