2013-05-30 38 views
0
<!DOCTYPE html> 
<html> 
<head> 
<title>Lesson 18: Making AJAX Calls</title> 
</head> 
<body> 
<h1>Lesson 18: Making AJAX Calls - Plain Text Response</h1> 
<div> 
<h2 id="myHeader">Click the button to call your data</h2> 
<input type="button" value="Click Me!" onclick="getText('test.txt')" /> 
</div> 
<script type="text/javascript"> 
var myRequest; 
function getText(url) 
     {   
      if (window.XMLHttpRequest)   
      {   
      myRequest = new XMLHttpRequest();   
      }   
      else   
      {   
      myRequest = new ActiveXObject("Microsoft.XMLHTTP");   
      } 
     myRequest.open("GET", url, true); 
     myRequest.send(null);  
     myRequest.onreadystatechange = getData;   
     } 
function getData()   
     { 
     var myHeader = document.getElementById("myHeader"); 
     if (myRequest.readyState ===4)   
      {   
     if (myRequest.status === 200)  
      { 
     var text = myRequest.responseText; 
     myHeader.firstChild.nodeValue = text;   
      }   
     }   
     }   
     </script>    
     </body> 
     </html> 

這段代碼是從本教程:http://www.html.net/tutorials/javascript/lesson18.php問題與阿賈克斯的send()函數

問題:

這是什麼意思:myRequest.send(NULL);它和myRequest.send();有什麼區別?

回答

0

沒有區別。 .send(null)表示您在請求正文中發送「null」內容。 .send()意味着你在請求正文中沒有發送任何內容。 在GET要求的情況下沒有區別,因爲請求主體沒有發送。 POST請求的情況下也不會有任何差異。

看到這個:Why do we pass null to XMLHttpRequest.send?