Native AJAX report 400 error

Source: Internet
Author: User
Tags string format

Native Ajax 400 error, the online generally said parameter error. It's always wrong to think that the parameter name is incorrect.

But the mistake I encountered was not the reason.

The background configuration of spring, and then the front-end I want to use native Ajax to submit data; When I receive the parameters, I receive them directly from a Java object.

 Public classPlan {Private intplanId; Private intFatherplanid; PrivateString Fatherplanname; PrivateString PlanName; Private inttask; Private intStartPage; Private intEndPage; PrivateDate StartDate; PrivateDate endDate;
@RequestMapping (value= "/savekeyword"},method=requestmethod.post) @ResponseBodypublicint Savekeyword (Plan plan) {

Error scenario 1:java The backend cannot convert the data from the front end into a Java built-in type other than the base type (with the wrapper type) and the string type (as you can see, I can receive data with my custom plan type, without the date type). But cannot receive data with date type)

1. Ajax GET Request:

At first, I used the Ajax get method to request data, directly with url+ "key1=value1&key2=value2" form to the server to send data, front-end and the background of the data sent to receive no problem;

2. Ajax POST Request:

When the data is submitted using the Ajax post method, the data is placed in the Send () method of the Ajax object, or in the form of "key1=" +var1+ "&key2=" +var2 (note that Chinese or other special characters need to be encodeURI (data)) , the result is always reported 400 error;

I then replaced the above Plan object with the data in each of the specific formats, as shown in the following code:

    @ResponseBody    publicint Saveplan (String planname,int  task,date Startdate,date endDate) {        log.info ("");        System.out.println ("planname=" +planname);        System.out.println ("task=" +Task);        System.out.println ("startdate=" +startdate);        System.out.println ("enddate=" +enddate);

The results still reported 400 errors for the following reasons:

September 12:59:52 for value ' 2017-09-11 '; Nested exception is java.lang.IllegalArgumentException

See why, the background cannot convert dates in string format to date types . Then I'll replace the date type of the received data with the string type try:

 Public int Saveplan (String planname,int  task,string startdate,string endDate) {        log.info ("" );        System.out.println ("planname=" +planname);        System.out.println ("task=" +Task);        System.out.println ("startdate=" +startdate);        System.out.println ("enddate=" +enddate);

The Ajax post is requested again and the results are as follows:

September 1:02:24 pm Study.read.controller.FirstTry saveplan info: planname= Netty Combat task=  StartDate= 2017-09-11endDate= 2017-09-19

It's perfectly normal!

If I replace the above data reception format (plan plan), and send the data, do not send the date type attribute corresponding parameter name, the result is normal

  

Native AJAX report 400 error

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.