2015-11-18 62 views
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(); 
+1

爲什麼不在[$ .ajax](http://api.jquery.com/jquery.ajax/)當你在頁面上加載jQuery – Tushar

+0

我是學習,所以我只是想知道原始語法第一 –

回答

1

if(xhr.readyState ==4)不是你有什麼

你還應該檢查xhr.statusonreadystatechange

所以,你的代碼將

var xhr = new XMLHttpRequest(); 
xhr.onreadystatechange = function() { 
    if (xhr.readyState == 4 && xhr.status == 200) { 
     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(); 

我更喜歡使用的onload/onError的

var xhr = new XMLHttpRequest(); 
xhr.onload = function() { 
    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.onerror = function(e) { 
    console.log(e); 
}; 
xhr.open('GET', 'json/main.json'); 
xhr.send(); 

但是onload/oner ror可能不適用於舊的瀏覽器,例如IE

+0

感謝加載工作:) –