0
iam在wamp服務器上測試ajax,但它說send()是一個匿名函數,並且iam正在獲取空白文檔。send()是一個匿名函數嗎?
控制檯登錄: XHR加載完成:GET 「http://localhost/json/main.json」(匿名函數)@ widget.js:21(這是xhr.send();
)
我的HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ajax</title>
<link rel="stylesheet" href="css/style.css" media="screen" title="no title" charset="utf-8">
</head>
<body>
<div id="employees">
</div>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="js/widget.js"></script>
<script type="text/javascript" src="json/main.JSON"></script>
</body>
</html>
的JSON對象:
[
{
"name": "Joe Consterdine",
"workstatus": true
},
{
"name": "Roberto Baggio",
"workstatus": true
},
{
"name": "Michael Smith",
"workstatus": false
},
{
"name": "Darren Huckerby",
"workstatus": true
},
{
"name": "Dean Blackwell",
"workstatus": false
},
{
"name": "Neil Sullivan",
"workstatus": false
},
{
"name": "Mark Fish",
"workstatus": true
},
{
"name": "Dean Holdsworth",
"workstatus": true
}
]
我的js代碼:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.onreadystate === 4){
var employees = JSON.parse(xhr.responseText);
var statusHTML = '<ul class="bulleted">';
for(var i = 0; i<employees.length; i++){
if(employees[i].workstatus === true){
statusHTML += '<li class="in">';
}else{
statusHTML += '<li class="out">';
}
statusHTML += employees[i].name + '</li>';
}
statusHTML += '</ul>';
document.getElementById('employees').innerHTML = statusHTML;
}
}
xhr.open('GET', 'json/main.json');
xhr.send();
爲什麼不在[$ .ajax](http://api.jquery.com/jquery.ajax/)當你在頁面上加載jQuery – Tushar
我是學習,所以我只是想知道原始語法第一 –