zoukankan      html  css  js  c++  java
  • 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中自定义属性
                        - 页面(***)
                    
            删除:
                对话框

  • 相关阅读:
    百度网盘下载速度慢的问题解决
    问题汇总
    centos 遇到Name or service not known
    centos7 下 python3 和python2 同时存在但是无法使用pip3 的解决方案
    pycharm2020(最简单的方法)配置远程连接服务器
    pycharm2020.1.2激活
    centos安装成功bart标注工具
    keras遇到bert实战一(bert实现分类)
    使用Array.slice(0) 实现数组浅拷贝
    try/catch/finally 语句
  • 原文地址:https://www.cnblogs.com/icemonkey/p/10533130.html
Copyright © 2011-2022 走看看