Ajax表单序列化后的数据格式转成Json发送给后台

    <script>
        $(function(){
        //表单转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;
                };
        //表单转json结束
            $('#add_po').click(function(){                 
                $.ajax({
                    url:"/scf/purchase/add.do", //接口地址
                    type:"post",
                    data:JSON.stringify($('#po_add_form1').serializeObject()) + '&' + JSON.stringify($('#add_po_form').serializeObject()),//将表单序列化成一个对象,这里多个表单相加
            //serializeObject()引入上述函数,将表单序列化成一个对象
            //JSON.stringify()将对象转成字符串 contentType:
'application/json', datatype:"json", success:function(data){ swal(data.message); //location.href="scm_po_fhqd.html"; },error: function(XMLHttpRequest) { console.log("错误状态:"+XMLHttpRequest.status); } }) }) }) </script>

 

posted @ 2016-11-14 09:15  God丶魔多  阅读(6810)  评论(0编辑  收藏  举报