Question about converting front-end javascript objects into java objects in the background _ javascript tips-js tutorial

Source: Internet
Author: User
Many times in the development project, I encountered the problem of converting front-end js objects into java objects in the background. The following describes how to convert front-end js objects to back-end java objects, interested friends can learn about the problem of converting front-end js objects to java objects in the background many times during the recent project development process. Record the practices to be used.

Simply put, the front-end uses the JSON. stringify () method to convert a js object to a js string. the backend receives a json string and converts it to a javaBean.

Front-end 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 );
});


Background code:

The Code is as follows:


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

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

......
}
}

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.