Home > Web Front-end > JS Tutorial > body text

Discussion on the problem of converting front-end js objects into java objects in the background_javascript skills

WBOY
Release: 2016-05-16 17:07:50
Original
1123 people have browsed it

During the recent project development process, I encountered the problem of converting the front-end js objects into java objects in the background many times. Record the method for later use.

To put it simply, use the JSON.stringify() method in the frontend to convert the js object into a js string, and receive the json string in the background and convert it into a javaBean.

Frontend code:

Copy code The code is as follows:

var data = {};
data.id = $('#id').val();
data.msg = $('#msg').val();

//Submit data
$.post(contextPath '/XXX.do?' new Date().getTime(),{data: JSON.stringify(data)},function(result){
alert(result);
});

Backend code:
Copy code The code is as follows:

@RequestMapping("/XXX")
public void save(HttpservletResponse response,String data){
if(!StringUtils.isEmpty(data)){

//json Convert string to javaBean
Msg msg = (Msg) JSONObject.toBean(JSONObject.fromObject(data),Msg.class);

......
}
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!