JS JSON序列化 Ajax form表单

# JS序列化
a = {"k1":"v1"}

#序列化为字符串 类似python json.dumps(a)
b = JSON.stringify(a)
"{\"k1\":\"v1\"}"

#序列化为字典 类似python json.loads(b)
c = JSON.parse(b)
Object { k1: "v1" }

1.
    Python序列化
        字符串 = json.dumps(对象)    对象->字符串
        对象 = json.loads(字符串)    字符串->对象
        
    JavaScript:
        字符串 = JSON.stringify(对象) 对象->字符串
        对象 = JSON.parse(字符串)     字符串->对象
        
    应用场景:
        数据传输时,
            发送:字符串
            接收:字符串 -> 对象
2. ajax

    $.ajax({
        url: 'http//www.baidu.com',
        type: 'GET',
        data: {'k1':'v1'},
        success:function(arg){
            // arg是字符串类型
            // var obj = JSON.parse(arg)
        }
    })
    
    
    $.ajax({
        url: 'http//www.baidu.com',
        type: 'GET',
        data: {'k1':'v1'},
        dataType: 'JSON',
        success:function(arg){
            // arg是对象
        }
    })
    
    
    $.ajax({
        url: 'http//www.baidu.com',
        type: 'GET',
        data: {'k1':[1,2,3,4]},
        dataType: 'JSON',
        success:function(arg){
            // arg是对象
        }
    })
    
    发送数据时:
        data中的v
        
        a. 只是字符串或数字
            $.ajax({
                url: 'http//www.baidu.com',
                type: 'GET',
                data: {'k1':'v1'},
                dataType: 'JSON',
                success:function(arg){
                    // arg是对象
                }
            })
        b. 包含属组
            $.ajax({
                url: 'http//www.baidu.com',
                type: 'GET',
                data: {'k1':[1,2,3,4]},
                dataType: 'JSON',
                traditional: true,
                success:function(arg){
                    // arg是对象
                }
            })
            
        c. 传字典
        
            b. 包含属组
            $.ajax({
                url: 'http//www.baidu.com',
                type: 'GET',
                data: {'k1': JSON.stringify({}) },
                dataType: 'JSON',
                success:function(arg){
                    // arg是对象
                }
            })
        
        d.传整个form表单
            
        $(function () {
            ajaxsumbit();
        });


        function ajaxsumbit() {
            $("#btn").click(function () {
                $.ajax({
                    url: "ajax1.html",
                    data: $("#fm").serialize(),  # form表单内容
                    type: "GET",
                    dataType: "json",
                    success: function (args) {
                        console.log(args.username);
                        console.log(args.passwd);
                        console.log(args.status);
                        $("#sp").text(args.status + " " + args.passwd + " " + args.username);
                    },
                    error: function () {
                        alert("error")
                    }
                })
            })
        }    
            
    
    
3. 事件委托

    $('要绑定标签的上级标签').on('click','要绑定的标签',function(){})

    $('要绑定标签的上级标签').delegate('要绑定的标签','click',function(){})

4. 编辑
    

5. 总结

        新URL方式:
            - 独立的页面
            - 数据量大或条目多
            
        对话框方式:
            - 数据量小或条目少
                -增加,编辑
                    - Ajax: 考虑,当前页;td中自定义属性
                    - 页面(***)
                
        删除:
            对话框

 

posted @ 2019-03-14 20:11  风追海浪  阅读(2679)  评论(0编辑  收藏  举报