java关于form表单的序列化提交(包括带有file)

来源:互联网 发布:钢琴软件电脑版 编辑:程序博客网 时间:2024/06/06 19:30

1.普通 form表单序列化提交

1):

var o = {};    var a = $('#form').serializeArray();//序列化为array      alert(a);//转json格式    alert(JSON.stringify(a));//转json格式    //将序列化陈的array  然后转化为 ajax格式的json  数据    $.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 || '';        }    });    alert(JSON.stringify(o));//转json格式1)  


2):

$("#form").data("bootstrapValidator").validate();            if (!$("#form").data("bootstrapValidator").isValid()) {                return;            } else{                $.ajax({                    url : createUrl('familyFileNum/fileNum/addfilNum'),                    type: 'POST',                    data:$('#form').serialize(),                    datatype:'json',                    success : function(msg){                        if(msg.success){                            layer.alert(msg.msg,{icon:1},function(){                                parent.$(".in.active").find("iframe").attr('src', parent.$(".in.active").find("iframe").attr('src'));                                parent.layer.closeAll();                            });                        }else{                            layer.alert(msg.msg,{icon:2});                        }                    },                    error : function(msg){                        layer.alert("系统发生错误,请稍后再试",{icon:5});                    }                });            }


2.带文件form 序列化

1):

var form = new FormData(document.getElementById("form"));    $.ajax({        url : createUrl('familyRegister/addRegister'),        data : form,        type : 'POST',        processData : false,        contentType : false,        success : function(msg) {            if (msg.success) {                layer.alert(msg.msg, {                    icon : 1                }, function() {                    parent.layer.closeAll();                    if (parent.$(".in.active").find("iframe").size()) {                        parent.$(".in.active").find("iframe").attr('src', parent.$(".in.active").find("iframe").attr('src'));                    } else {                        parent.window.location.reload();                    }                });            } else {                layer.alert(msg.msg, {                    icon : 2                });            }        },        error : function(msg) {            layer.alert("系统发生错误,请重新登录或者稍后再试", {                icon : 5            });        }    });




2):

$('#form').ajaxSubmit({                success : function(msg){                    if(msg.success){                        layer.alert(msg.msg,{icon : 1},function(){                            parent.$(".in.active").find("iframe").attr('src', parent.$(".in.active").find("iframe").attr('src'));                            parent.layer.closeAll();                        });                    }else{                        layer.alert(msg.msg,{icon:2});                    }                },                error : function(msg){                    layer.alert("系统发生错误,请重新登录或者稍后再试",{icon:5});                }            });



0 0
原创粉丝点击