Ajax JSON interaction and SPRINGMVC in @requestbody

Source: Internet
Author: User

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

Related Article

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.