您的位置:首页 > Web前端 > JavaScript

js封装from表单数据为json串进行ajax提交

2017-01-06 00:00 531 查看
摘要: js封装from表单数据为json串进行ajax提交

json封装代码

function getFormJson(frm) {  //frm:form表单的id
var o = {};
var a = $("#"+frm).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;
}


返回的数据格式为标准的json格式,ajax使用如下:

$.ajax({
type: 'post',
url: 'your url',
data: getFormJson(frm),
success: function(data) {
// your code
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java ajax json
相关文章推荐