js原生读取json

function showJson(){  
    var test;  
    if(window.XMLHttpRequest){  
        test = new XMLHttpRequest();  
    }else if(window.ActiveXObject){  
        test = new window.ActiveXObject();  
    }else{  
        alert("请升级至最新版本的浏览器");  
    }  
    if(test !=null){  
        test.open("GET","points.json",true);  
        test.send(null);  
        test.onreadystatechange=function(){  
            if(test.readyState==4&&test.status==200){ 
                console.log(test.responseText);
    //          var obj = JSON.parse(test.responseText);  
    //          for (var name in obj){  
    //              alert(obj[name].key);  
    //          }  
            }  
        };  
      
    }  
}  
window.onload=function(){  
  showJson();  
}

 

posted @ 2017-09-28 18:03  Redchar  阅读(2327)  评论(0编辑  收藏  举报