原生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>