The request sent by the client was syntactically incorrect.
Ajax initiates the request for the Times 400 error. The request code is as follows:
varReportid= (obj). Parent (). Parent (). Children (": First"). attr ("value")); varischecked=$ (obj). Prop ("checked") = = "Checked"? 1:0; varreportsetting=$ (obj). attr ("value"); varsetting={reportid:reportid,ischecked:ischecked,reportsetting:reportsetting}; Console.log (json.stringify (setting)); $.ajax ({type:"POST", URL:"/reportconfiguration", ContentType:"Application/json", data:JSON.stringify (setting), DataType:"JSON", Success:function(msg) {if(msg.issuccess) {$ ("#msg"). HTML ("Set success") }Else{ $("#msg"). HTML (Msg.result); } } });
Service-Side code:
@RequestMapping ("/reportconfiguration") @ResponseBody public String reportset (@ Requestbody Reportsettingeditbean reportsettingeditbean,httpservletrequest request) { return "" ; }
Bean definition:
Public classReportsettingeditbean {Private LongReportID; Private BooleanisChecked; Privatereportsetting reportsetting; Public LongGetreportid () {returnReportID; } Public voidSetreportid (LongReportID) { This. ReportID =ReportID; } Public BooleanisChecked () {returnisChecked; } Public voidSetchecked (BooleanisChecked) { This. isChecked =isChecked; } Publicreportsetting getreportsetting () {returnreportsetting; } Public voidsetreportsetting (reportsetting reportsetting) { This. reportsetting =reportsetting; }} Public enumreportsetting {Fixed (1), Scroll (2), First (4); Private intvalue; Public intGetValue () {returnvalue; } reportsetting (intvalue) { This. value=value; }}
Solve:
Verify in JS that the data type is consistent with the data type of the attribute in the receiving data class.
In the above example: Reportsetting is an enumeration object, and Var reportsetting=$ (obj). attr ("value") is a string. can be changed into integers. The correct request is as follows:
varReportid= (obj). Parent (). Parent (). Children (": First"). attr ("value")); varischecked=$ (obj). Prop ("checked") = = "Checked"? 1:0; var reportsetting=parseint ($ (obj). attr ("value")); varsetting={reportid:reportid,ischecked:ischecked,reportsetting:reportsetting}; $.ajax ({type:"POST", URL:"/reportconfiguration", ContentType:"Application/json", data:JSON.stringify (setting), DataType:"JSON", Success:function(msg) {if(msg.issuccess) {$ ("#msg"). HTML ("Set success") }Else{ $("#msg"). HTML (Msg.result); } } });
Spring MVC Ajax 400 Solution