<!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
问题:
这是什么意思: myRequest.send(null);它和myRequest.send();有什么区别?
发布于 2013-05-30 17:41:34
没什么区别。.send(null)表示您在请求正文中发送的内容为空。.send()表示在请求体中不发送任何内容。在GET请求的情况下没有区别,因为没有发送请求正文。在POST请求的情况下,也没有区别。
https://stackoverflow.com/questions/16832488
复制相似问题