Form form converted to JSON string data

Source: Internet
Author: User

Https://github.com/marioizquierdo/jquery.serializeJSON

Load Using
<script type= "Text/javascript" src= "jquery.js" ></script><script type= "Text/javascript" src= " Jquery.serializejson.js "></script>
Form Form page code
<form id= "Formdemo" class= "Layui-form" > <table class= "layui-table" lay-even> <colgroup> <col width= > <col width= "$" > <col> </colgroup> &lt                    ;tbody> <tr> <th> user name </th> <td id= "UserName" > <div class= "Layui-form-item layui-input-inline" > <input tyep= "text" Name= "Userna                                    Me "class=" Layui-input "> </div> </td> </tr>                    <tr> <th> Login Password </th> <td id= "Password" > <div class= "Layui-form-item layui-input-inline" > <input tyep= "text" Name= "pass                        Word "class=" layui-input "> </div> </td> </tr> <tr>               <th> user Rights </th> <td id= "role" > <div class= "layui-form- Item Layui-input-inline "> <input type=" text "name=" role "class=" Layui-input "readonly=" true "va                Lue= "Admin" > </div> </td> </tr> <tr>  <th> phone number </th> <td id= "Phone" > <div class= "Layui-form-item                    Layui-input-inline "> <input tyep=" text "name=" Phone "class=" Layui-input "> </div> </td> </tr> <tr> <th> User Comments <                        /th> <td id= "nickname" > <div class= "Layui-form-item layui-input-inline" >                <input tyep= "text" name= "nickname" class= "Layui-input" > </div>   </td>         </tr> </tbody> </table> </form> 
Serialization of
<script type= "Text/javascript" >    function GetUser () {        Console.log ($ (' #formDemo '). Serializejson ());        Console.log (Json.stringify ($ (' #formDemo '). Serializejson ()));        Return Json.stringify ($ (' #formDemo '). Serializejson ());    } </script>
$ (' #formDemo '). Serializejson () returns an object body
{userName: "Test", Password: "Test", Role: "admin", Phone: "18888888866", Nickname: "Test"}
Json.stringify ($ (' #formDemo '). Serializejson ()) returns a JSON string
{"UserName": "Test", "Password": "Test", "role": "Administrator", "Phone": "18888888866", "nickname": "Test"}
use Ajax to pass to the background using @requestbody to receive
<script type= "Text/javascript" >    function AddUser () {        var data = GetUser ();        $.ajax ({         type: "POST",         URL: "${ctx}/auth/user/create",         data:data,         dataType: "JSON",         ContentType: "Application/json",         success:function (data) {            alert (data.message);             if (data.code==0) {                 layer.closeall ();                 Window.location.href= "${ctx}/auth/users";}}        );    }    </script>

to get the values in a key-value pair in JS, you can do the following
<script type= "Text/javascript" >    var data = GetUser ();    var obj = eval ("(" +data+ ")");    alert (obj.username);    </script>

Form form converted to JSON string data

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.