• 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中自定义属性
                        - 页面(***)
                    
            删除:
                对话框

  • 相关阅读:
    ubuntu配置apache支持sqlite数据库
    linux中的(),(()),[],[[]],{}的作用
    javascript问题积累
    羞愧的开始,成熟的开始,努力的开始
    asp发送邮件代码
    css&html的问题积累
    应用phpcms时遇到的问题及smarty标签的应用
    js正则积累
    产生一个int数组,长度为100,并向其中随机插入1100,并且不能重复。按照数组下标输出结果。
    不同项目之间的基础dll共用问题
  • 原文地址:https://www.cnblogs.com/icemonkey/p/10533130.html
Copyright © 2020-2023  润新知