Ajax JSON interaction and SPRINGMVC in @requestbody
Background
Provide it yourself. The parameters in the interface are set to @requestbody Vippromotionlog Vippromotionlog as an object. But the front-end personnel have to deal with the code as follows
var data = { "userId" : 20142100122, "userOperationStep" : 2, "appPlatform": "android", "app" : 0, "videoId":123123 }; $.ajax({ url : ‘http://localhost:81/online-2c/api/vippromotion‘, type : ‘POST‘, dateType : ‘json‘, data:data, success : function(msg){ console.log(msg); } })
Problem arises
An error occurred in the above situation
Figure A
Investigation
View this from request type two
Figure II
Content-type to Application/x-www-form-urlencoded
And the transmission of the past type is an object, then what we set the @requestbody need is not an object. My personal understanding here is that @requestbody needs to transfer the past is a string and corresponds to the property one by one of its annotated object.
Workaround One
There are two ways to resolve the first without changing the background code in the case of the following code
var data = { "userId" : 20142100122, "userOperationStep" : 2, "appPlatform": "android", "app" : 0, "videoId":123123 }; $.ajax({ url : ‘http://localhost:81/online-2c/api/vippromotion‘, type : ‘POST‘, dateType : ‘json‘, contentType : ‘application/json‘, data:JSON.stringify(data), success : function(msg){ console.log(msg); } })
Application/json transmits the past data is a string of JSON object, data is an object, with Json.stringify converted to object string, in fact, the above code is the same as the following
var data = ‘{\n‘ + ‘ "userId" : 20142100122,\n‘ + ‘ "userOperationStep" : 2,\n‘ + ‘ "appPlatform": "android",\n‘ + ‘ "app" : 0,\n‘ + ‘ "videoId":123123\n‘ + ‘ }‘; $.ajax({ url : ‘http://localhost:81/online-2c/api/vippromotion‘, type : ‘POST‘, dateType : ‘json‘, contentType : ‘application/json‘, data:data, success : function(msg){ console.log(msg); } })
Method II
Modify the background code to remove @requestbody annotations. In this case, you transmit the past is an object, and one after another, if not the words are empty. The front-end JS code does not change.
Prevent
Select based on the type of data interaction for the day before and after.
Ajax JSON interaction and SPRINGMVC in @requestbody