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

Ajax xmlHttp.responseText 返回值
我在用ajax时遇到如下问题,见下面js代码的红字部分.不知道怎么得到正确的值.求解释 
var IsCode;
      var xmlHttp=GetXmlHttpObject();
      if (xmlHttp==null)
      {
       alert("您的浏览器不支持AJAX");
       return;
      }      
       xmlHttp.onreadystatechange=function()
      {
        if(xmlHttp.readyState==4)
        {
          if(xmlHttp.status==200)
          {
             IsCode=xmlHttp.responseText;    
             alert(IsCode);                             /// 这里得到的是整个HTML代码
           }
        //    document.getElementById("IDinfo").innerHTML=xmlHttp.responseText;      
        }
      }
          xmlHttp.open("POST","<%=request.getContextPath()%>/jsp/IDinfo.jsp?req=2&code="+document.getElementById("code").value,true);
          xmlHttp.send(null);
         
      alert(IsCode);                         //这里得到的却是undefined


IDinfo.jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<jsp:useBean id="register" scope="session"
class="edu.tongji.sse.p082940.lxj.javabean.Register"></jsp:useBean>

<!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">
-->
  </head>
  
  <body> <% if(request.getParameter("req").equals("1")) {%>
  <font size="2" style="color:red;">&a