Recently, when doing web development, using $.post to submit data, but the callback function has not been triggered, press F12 to see the console output is: POST * * * Bad Request
The background is SPRINGMVC, and setting breakpoints will not be triggered.
Later, the jquery data learned that $.post submit data only when the callback function is successful, so instead of submitting the data $.ajax, add the error callback function, get the error message, the following figure:
What is the cause of this problem?
It was later found that the content data type submitted by the form does not match the data type of the entity (that is, the data table field).
The user input should be validated before submitting the form, and the background is mapped directly, without the opportunity to do content validation. 2016-05-26 Update
Workaround:
The above only describes the cause of the problem, and did not give a solution, many small partners actually see the reason has already thought of the solution.
That some of the partners may have just contacted, still confused the situation, I will repeat the steps to resolve:
1, instead of $.ajax submit data, add error callback function (in fact, in the development of strictly speaking is not allowed to use the $.post, because the function of the loss of error handling);
Similar code is as follows, please refer to Jqurey reference manual:
$.ajax ({
type: "POST",
URL: "some.php",
data: "Name=john&location=boston",
success:function ( msg) {
alert ("Data Saved:" + msg);
},
error:function (xmlhttprequest) {
alert ("Error:" + xmlhttpreques T.responsetext);
}
);
This allows you to learn what is wrong with the back-end code through Xmlhttprequest.responsetext or xmlhttprequest.responsehtml.
2. Do not give the work of the generating entity to the MVC framework to complete
Your problem-generating code, I guess, might resemble the following:
/**
* Add user.
**/
@RequestMapping ("/adduser.do")
@ResponseBody public
void AddUser (HttpServletRequest request, HttpServletResponse response, User user) throws Exception {
this.userManager.saveOrUpdate (user);
}
This leads to the above mentioned problem: the background mapping directly, there is no opportunity to do content validation; Clearly a field requires a number, resulting in the user entered the letter or Chinese characters.
The correct approach is similar to the following:
/**
* Add user.
**/
@RequestMapping ("/adduser.do")
@ResponseBody public
void AddUser (HttpServletRequest request, HttpServletResponse response) throws Exception {
//To get the parameter user to the function internal definition create
User user = new user ();
This is the age of the user to be judged, the specific rules you define, I just give an example of the
Integer ages = Getinteger (the "Age");
if (age!= null) {
uset. Setage (age);
}
This.userManager.saveOrUpdate (user);
}
Protected Integer Getinteger (string name) {
string str = this.request.getParameter (name);
if (Stringutils.isnotblank (str)) {return
integer.valueof (str);
}
return null;
}
Have you solved the problem?
No.
Look at the following case:
Springboot Project Ajax post occurrence bad Request error background debugging non-breakpoint solution