zoukankan      html  css  js  c++  java
  • JavaScript json&ajax

    <!doctype html>
    <html lang='en'>
    <head>
        <title>JSON</title>
        <script src="jquery-1.11.0.min.js"></script>
        <script>
        window.onload = function(){
            var obj = {
                name:'sonia',
                age:20
            };
            //json格式 
            //1)JSON中对象要求给属性添加双引号
            //2)末尾没有分号
            //3)同一个对象中绝对不允许出现两个同名属性
            {
                "name":"sonia",
                "age":20
            }
            {
                "name":"lily",
                "hobby":["sing","dance","game"],
                "work":{
                    "job":"web开发",
                    "address":"wulan"
                },
                "member":[{
                        "name":"ben1",
                        "hobby":["game","sing"],
                        "member":[{
                            "name":"ben1-1",
                            "hobby":["game","sing"]
                        },{
                            "name":"ben1-2",
                            "hobby":["game","sing"]
                        }]
                    },
                    {
                        "name":"ben2",
                        "hobby":["game","sing"],
                        "member":[{
                            "name":"ben2-1",
                            "hobby":["game","sing"]
                        },{
                            "name":"ben2-2",
                            "hobby":["game","sing"]
                        }]
                    },
                    {
                        "name":"ben3",
                        "hobby":["game","sing"],
                        "member":[{
                            "name":"ben3-1",
                            "hobby":["game","sing"]
                        },{
                            "name":"ben3-2",
                            "hobby":["game","sing"]
                        }]
                    }]
            }
    
            //JSON.strinfify 过滤
            var data =  {
                "name":"lily",
                "hobby":["sing","dance","game"],
                "work":{
                    "job":"web开发",
                    "address":"wulan"
                },
                "member":["ben1","ben2","ben3"]
            };
            console.log(JSON.stringify(data,['name','hobby']));
    
            //AJAX
            //XMLHttpRequest   XHR   ActiveXObject (ie5  ie6)
            //1、创建XHR对象   2、连接服务器 3、发送请求   4、接收响应
            //var xhr = new XMLHttpRequest();
            var xhr;
            if(window.XMLHttpRequest){
                xhr = new XMLHttpRequest();
            }else {
                xhr = new ActiveXObject("Microsoft,XMLHTTP");  //处理IE5 6兼容性
            };
            xhr.open('get','get.asp?name=lily&age=20',true);  //请求类型   true 异步  false同步  get post put delete
            //xhr.send();
            xhr.send(JSON.stringify({"name":"sonia"}))  //post请求传入string
            xhr.onreadystatechange = function(){   //readystate变化会触发
                if(xhr.readyState ==4 && xhr.status == 200){
                    console.log(xhr.responseText)  //响应数据
                }
            }
    
    
    
        }
        </script>
    </head>
    <body>
        <ul>
            <li>a</li>
            <li>b</li>
            <li>c</li>
            <li>d</li>
        </ul>
    </body>
    </html>
    

    本博客所有文章仅用于学习、研究和交流目的,欢迎非商业性质转载。

    博主的文章没有高度、深度和广度,只是凑字数。由于博主的水平不高,不足和错误之处在所难免,希望大家能够批评指出。

    博主是利用读书、参考、引用、抄袭、复制和粘贴等多种方式打造成自己的文章,请原谅博主成为一个无耻的文档搬运工!

  • 相关阅读:
    安装git工具在ubuntu系统
    Ubuntu 16.04安装JDK并配置环境变量-【小白版】
    【gRPC使用问题4】
    【gRPC使用问题3】生成出来无法识别Google.Api.AnnotationsReflection.Descriptor
    LNMP
    Centos下安装Mysql
    yum方式安装的Apache目录详解和配置说明
    Centos下 yum方式安装LAMP
    CentOS配置网易163 yum源
    Apache主配置文件httpd.conf 详解
  • 原文地址:https://www.cnblogs.com/Dewumu/p/14430535.html
Copyright © 2011-2022 走看看