`
shirlly
  • 浏览: 1625427 次
  • 性别: Icon_minigender_2
  • 来自: 福州
社区版块
存档分类
最新评论

反应AJAX的基本执行过程的一个简单的例子

    博客分类:
  • AJAX
阅读更多
发送请求页面的代码
<html>
 <head>
  <title>ajax</title>
  <script language="javascript">
  /* Create a new XMLHttpRequest object to talk to the Web server */
	var xmlHttp = false; 
	try {
	  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	  try {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (e2) {
		xmlHttp = false;
	  }
	} 
	
	//发送AJAX请求
	function callServer() 
	{
	  // Get the city and state from the web form
	  var city = document.getElementById("city").value; 
	  // Only go on if there are values for both fields
	  if ((city == null) || (city == "")) return; 
	  // Build the URL to connect to
	  var url = "returnvalue.txt";//这里可以是一个xml文件,一个页面等
	  // Open a connection to the server
	  xmlHttp.open("GET", url, true);
	  // Setup a function for the server to run when it's done
	  xmlHttp.onreadystatechange = updatePage;
	  // Send the request
	  xmlHttp.send(null);
	}
	
	function updatePage() 
	{
	  if (xmlHttp.readyState == 4) {
		var response = xmlHttp.responseText;
		document.getElementById("returnValue").value = response;
	}
}


</script>

 </head>
<body>
 	<input id="city" type="text" onBlur="callServer();">
	<input id="returnValue" type="text">
</body>
</html>

执行的效果是将文本文件的内容返回到
<input id="returnValue" type="text">显示
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics