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

ajax传统的方式制作ajax

//这是前台的代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 <script type="text/javascript">
  /*onload = function (){
    //这是判断浏览器是IE还是FireFox
    if(navigator.appName == 'Netscape'){
     alert('这是火狐浏览器');
    }else if(navigator.appName == 'Microsoft Internet Explorer')
    {
     alert('这是IE浏览器');
    }
     if(window.XMLHttpRequest)//判断浏览器是否属于Mozilla,Sofari
       {
           alert("firefox");
       }
        else if(window.ActiveXObject)//判断浏览器是否属于IE
       {
    alert('这是IE浏览器');
    }
   }*/
   
   
   
   //这是最传统的ajax
   //第一步:创建核心对象
   onload = function(){
   var xmlRequest = null;
   if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
       xmlRequest=new XMLHttpRequest();
      }
   else if(window.ActiveXObject)
     {// code for IE6, IE5
    
      xmlRequest=new ActiveXObject("Microsoft.XMLHTTP");
     }
   if(!xmlRequest)
   {
     
    alert("创建核心对象错误!!!");
    return fasle;
   }
   //第二部:打开并发送请求
   //1、这是get发送请求的方式,并且还带有参数
   /*
    第一个参数是请求的方式,最好的大写,第二个参数是请求的路径(这里的路径千万不要用到${pageContext,request.contextPath),也不要
    在前面加上'/',例如:/test?p=test&namecaohuan,这也会访问不到路径,第三个参数表示是异步进行还是同步进行,XMLHttpRequest
    对象如果要用于AJAX的话其open()方法的第三个参数必须设置为 true,一般我们ajx都是进行异步的操作,所以一般是true
   */
  /* xmlRequest.open('GET','test?p=test&name=caohuan',true);
   //发送请求,get发送请求并带有参数send中并不带有参数,只有post请求才带有参数
   xmlRequest.send();*/
   //2、这是post发送请求,并且带有参数
   xmlRequest.open('POST','test?p=test',true);
   //注意这里post发送请求带有参数的形式,要用post发送请求带有参数,必须要加上下面的第一行的代码,否则后台根本接收不到参数
   xm