ajax

原生ajax

<button onclick="getData()">获取数据</button>
<p id="js_msg"></p>

<script>

    function getData(){
        loadXMLDoc("get","./test.json",function(data){
            console.log(data)
            console.log(typeof data)
//            console.log(typeof eval("("+data+")"))
//            var _data = eval("("+data+")")
            document.querySelector("#js_msg").innerHTML = data.name;
        })
    }

    function loadXMLDoc(method,url,callback){
        var xmlhttp;
        if(window.XMLHttpRequest){
            xmlhttp = new XMLHttpRequest();
        }else{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
        }
        xmlhttp.open(method,url,true);
        xmlhttp.send();
        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
//                callback(xmlhttp.responseText)
                callback(eval("("+xmlhttp.responseText+")"))
            }
        }
    }
</script>
posted on 2016-08-23 15:12  借个火点烟  阅读(123)  评论(0编辑  收藏  举报