html form表单数据转为json传输

html form表单数据转为json传输

from 表单

<form id="form1" name="form1" method="post" action="/infoSearch/getRestult">
                <table class="tableForm" style="width:100%; height:100%">
                    <tr>
                        <th>手机卡码(IMSI)</th>
                        <td>
                            <input id="imsi" name="imsi" class="easyui-textbox easyui-validatebox"
                                   data-options="prompt:''" style="width:285px;height:32px; margin:0 auto;">
                        </td>
                        <th>电话号码</th>
                        <td>
                            <input id="phonenum" name="phonenum" class="easyui-textbox"
                                   style="width:283px;height:32px; margin:0 auto;">
                        </td>
                    </tr>
                 </table>
</from>

json转换功能函数

$.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

    function submitForm() {
        alert("s");
        // $('#form1').submit();
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/infoSearch/getResult",
            data:JSON.stringify($("#form1").serializeObject()),
            success: function (res) {
                console.log(res);
            },
            error: function (res) {
                console.log(res)
            }
        });

前端form向后台提交信息

400错误
The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

出现这个错误的主要原因是:form 表单中和实体类中的字段无法对应。
下面是form表单中传送的json字符串,而实体类中的字段名称与json字符串中的字段名称完全一样,就会出现这种字段无法对应的情况。正确的做法是应该将实体类汇总的字段名称改为驼峰形式,若phonehome_str字段,在实体中应该设置为phonehomeStr.
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值