首页 > Web开发 > 详细

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

时间:2016-11-14 09:56:03      阅读:274      评论:0      收藏:0      [点我收藏+]
    <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>

 

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

原文:http://www.cnblogs.com/wangdahai/p/6060668.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!