当前位置: 移动技术网 > IT编程>网页制作>CSS > JSON和Ajax解析

JSON和Ajax解析

2018年09月19日  | 移动技术网IT编程  | 我要评论

json和ajax

一、json序列化和解析

    <script>
        var person = {
            name: "tf",
            age: 22,
            body: {
                height: 172,
                weight: 60
            }
        }
        var personjson = json.stringify(person);
        console.log(personjson);
        var personjson2 = json.stringify(person, function (key, value) {
            switch(key){
                case "height":
                    return value + "cm";
                case "weight":
                    return value + "kg";
                default:  // 必须要有
                    return value;
            }
        })
        console.log(personjson2);
        var personjson3 = json.stringify(person, ["name", "age"], 4);
        console.log(personjson3);

        var personobj = json.parse(personjson2);
        console.log(personobj);
    </script>

二、ajax请求

    <script>
        var xhr = new xmlhttprequest();
        xhr.onreadystatechange = function () {
            if(xhr.readystate == 4){
                if(xhr.status >= 200 && xhr.status = 200 && xhr2.status 

三、jsonp跨域

    <script>
        var scr = document.createelement("script");

        scr.src = "https://suggest.taobao.com/sugcode=utf-8&q=%e5%8d%ab%e8%a1%a3&callback=cb";
        document.body.insertbefore(scr, document.body.firstchild);

        function cb(response) {
            console.log(response);
        }
    </script>

即通过script标签的src进行跨域访问接口,src参数包含一个回调函数(上为cb),接受参数为响应体。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网