利用Jquery的post来提交数据到后台

版权声明:@渔闻520 https://blog.csdn.net/weixin_41060905/article/details/87521723
 <script src="https://code.jquery.com/jquery-3.2.0.js"></script>
    <!--
            此处插入JavaScript脚本
    -->
    <script type="text/javascript">

        /** 传递JSON**/

        $(document).ready(function() {
            //JSON参数和类RoleParams一一对应

            var data = {
                //角色查询参数
                roleName : 'role',
                note : 'note',
                //分页参数
                pageParams : {
                    start : 0,
                    limit : 3
                }
            }
            //Jquery的post请求
            $.post({
                url : "http://localhost:8080/Chapter14/role/findRoles.form",
                //此处需要告知传递参数类型为JSON,不能缺少
                contentType : "application/json",
                //将JSON转化为字符串传递
                data : JSON.stringify(data),
                //成功后的方法
                success : function(result) {
                }
            });
        });

    </script>

还可以通过表单序列化,即将表单数据转换为字符串传递给后台,即在用户点击提交按钮之后,通过序列化来提交表单。

$("form").serialize();

注释:只会将”成功的控件“序列化为字符串。如果不使用按钮来提交表单,则不对提交按钮的值序列化。如果要表单元素的值包含到序列字符串中,元素必须使用 name 属性。

其会生成标准的url查询字符串,即a=?&&b=? 类似这样的形式。

猜你喜欢

转载自blog.csdn.net/weixin_41060905/article/details/87521723