美文网首页
json 文件 和 公开接口

json 文件 和 公开接口

作者: 会说话的乌鸦 | 来源:发表于2018-07-30 18:52 被阅读0次

    json
    json格式的数据:

    {
    "name":'tom',
    "age":18
    }
    与json对象不同的是,json数据格式的属性名称需要用双引号引起来,用单引号或者不用引号会导致读取数据错误。
    json的另外一个数据格式是数组,和javascript中的数组字面量相同。
    ['tom',18,'programmer']

    公开接口的运用

    <script type="text/javascript">
            //360搜索的公开接口
            //https://sug.so.360.cn/suggest?callback=suggest_so&encodein=utf-8&encodeout=utf-8&format=json&fields=word&word=s
    
            $(function(){
                $('#txt01').keyup(function(){
                    var val = $(this).val();
    
                    $.ajax({
                        url: 'https://sug.so.360.cn/suggest?',//请求360搜索的公开接口
                        type: 'get',
                        dataType: 'jsonp',//跨域请求
                        data: {word: val}//携带参数
                    })
                    .done(function(data) {
                        console.log(data);
                        // alert(data.s.length);//10条数据
    
                        $('.list').empty();//先清空列表
    
                        //模拟搜索联想,循环插入新列表
                        for(var i=0; i<data.s.length; i++){
                            var $li = $('<li>'+data.s[i]+'</li>');
                            $li.prependTo('.list');
                        }
                    })
                    .fail(function() {
                        console.log("error");
                    });
                })
            })
            
        </script>
    

    相关文章

      网友评论

          本文标题:json 文件 和 公开接口

          本文链接:https://www.haomeiwen.com/subject/befqvftx.html