<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>json实战</title>
</head>
<body>
<ul id="myDiv">
<li>111</li>
</ul>
<button id="btn">加载</button>
</body>
<script type="text/javascript">
window.onload = function(){
var Btn = document.getElementById("btn");
Btn.addEventListener('click',function(){
chuFa();
})
}
function chuFa() {
var myXMLHttpRequest = null;
if (window.ActiveXObject) {
myXMLHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} else {
myXMLHttpRequest = new XMLHttpRequest();
}
var url = "http://192.168.1.35:8860/helloJson.json";
var data = "key=val";
myXMLHttpRequest.open("get", url, true);
myXMLHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
myXMLHttpRequest.onreadystatechange = function(){
if (myXMLHttpRequest.readyState == 4 && myXMLHttpRequest.status == 200) {
var res = myXMLHttpRequest.responseText;
var jsonObj = eval("("+res+")");
for(var i=0; i < jsonObj.length; i++){
var info = "<li>"+ jsonObj.name +"</li>";
document.getElementById("myDiv").appendChild(info);
}
}
}
myXMLHttpRequest.send(data);
}
</script>
</html>
4 回答
Joy_Sang
TA贡献64条经验 获得超13个赞
var info = document.createElement('li'); info.textContent = jsonObj[i].name document.getElementById("myDiv").appendChild(info);
添加回答
举报
0/150
提交
取消