摘要: 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
  }
});


感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com