ajax的post请求

来源:互联网 发布:zip压缩算法 编辑:程序博客网 时间:2024/05/04 08:10

index.jsp:

<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>123</title>
<script language="javascript">  
   //定义一个变量用于存放XMLHttpRequest对象   
   var xmlHttp;
   //改函数用于创建一个XMLHttpRequest对象   
   function createXMLHttpRequest(){   
       if(window.ActiveXObject){   
           xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");   
       }else if(window.XMLHttpRequest){   
           xmlHttp = new XMLHttpRequest();   
       }   
   }

   //这是一个启动AJAX异步通信的方法   
   function ajaxLogin(){
       var ln = document.getElementById("returnMes").value; 
       //创建一个XMLHttpRequest对象
       createXMLHttpRequest();
       //将状态绑定到一个函数   
       xmlHttp.onreadystatechange=processAjaxLogin;   
       //通过GET方法向指定的URL建立服务器的调用   
       var url="ajaxLogin.action";
       xmlHttp.open("post",url,true);
       //设置请求头
       xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
       //发送请求   
       xmlHttp.send("responseText="+ln);  
   }   
   //这是一个用来处理状态改变的函数   
   function processAjaxLogin(){   
       //定义一个变量用于存放 从服务器返回的响应结果   
       var responseContext="";   
       if(xmlHttp.readyState==4){
           if(xmlHttp.status==200){
               responseContext = xmlHttp.responseText;
               document.getElementById("returnMes").value=responseContext;
           }   
       }
   }
</script>
</head>
<body>
<input type="text" id="returnMes">       
<input type="button" value="异步请求" onclick="ajaxLogin();">
<table>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
</table>
</body>
</html>

 

其余代码同Ajax的get请求

原创粉丝点击