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.