日期:2014-05-16  浏览次数:20739 次

知道ajax后 第一个 超级简单的 入门程序



[size=medium][/size]

客户端    jsp界面

<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>ajax 入门示例</title>
<script language="javascript">
var http_request = false;
function send_request() {
  http_request = false;//开始初始化XMLHttpRequest对象
  if(window.XMLHttpRequest) { //Mozilla 浏览器
   http_request = new XMLHttpRequest();
;
  }
  else if (window.ActiveXObject) { // IE浏览器
     http_request = new ActiveXObject("Microsoft.XMLHTTP");
      }

  http_request.onreadystatechange =function () {
        if (http_request.readyState == 4&&http_request.status == 200) { // 判断对象状态及是否成功返回
                alert(http_request.responseText);
                 }
                 }

http_request.open("get","hh.jsp",true);//
http_request.send();
}

</script>
</head>

<body>
<form name="form1" method="post"  >
        用户名
<input name="username" type="text" id="username" size="20">
mima
<input name="password" type="password" id="password" size="20">
<input type="button" name="Submit"  onclick="send_request()"  value="提交">
</form>
</body>
</html>

服务器端jsp界面 hh.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
out.println("succeeed:" );
%>

弹出 succeeed的提示窗 证明 异步通信成功!!!