当前位置: 代码迷 >> Ajax >> innerHTML=http.responseText,结果老是一闪而过,咋办
  详细解决方案

innerHTML=http.responseText,结果老是一闪而过,咋办

热度:1091   发布时间:2012-09-18 16:21:42.0
innerHTML=http.responseText,结果总是一闪而过,咋办?
小妹写了个AJAX程序,jsp提交参数到java脚本后台,得到的查询结果返回给jsp,结果是有了,就是一闪而过。我把代码贴在下面,麻烦大虾们帮我看看~,多谢了!!
JScript code
<!DOCTYPE html>
<html>
  <head>
    <title>search-Cp.html</title>
    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
 <script type="text/javascript">


var http_request = false;
function send_request(url) {//初始化、指定处理函数、发送请求的函数
  http_request = false;
  //开始初始化XMLHttpRequest对象
  if(window.XMLHttpRequest) { //Mozilla 浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//设置MiME类别
http_request.overrideMimeType('text/xml');
}
  }else if (window.ActiveXObject) { // IE浏览器
try{
    http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
      http_request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {}
}
  }
if (!http_request) { // 异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}
http_request.onreadystatechange = processRequest;
// 确定发送请求的方式和URL以及是否同步执行下段代码
http_request.open("GET", url, true);
http_request.send(null);
}
// 处理返回信息的函数
function processRequest() {
if (http_request.readyState == 4) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
var kk=document.getElementById("txtHint");
   kk.style.display="block";
   document.getElementById("txtHint").innerHTML=http_request.responseText;
    
} else { //页面不正常
    alert("您所请求的页面有异常。");
}
}
}
function search() {
  var f=document.form2;
  var element1 = f.element.value;
  if(element1=="") {
window.alert("The user name can not be null!");
f.element.focus();
return false;
  }else {
send_request('check.jsp?element='+element1);
  }
}

</script>

<body><form name="form2" method="post" action="">
  <table width="100%" border="1">
    <tr>
      <td width="200" height="25"><div align="right">请选择元素Element:</div></td>
      <td><input type="text" name="element" id="element" /></td>
    </tr>
    <tr>
      <td height="25"><div align="right">请选择相phase:</div></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><div align="right">请输入温度:</div></td>
      <td><input type="text" name="T" id="T" /></td>
    </tr>
  </table>
  <table width="100%" border="1">
    <tr>
      <td><div align="center">
        <input type="reset" name="reset" id="reset" value="清空" /> 
        <input type="submit" name="button" id="button" value="查询该元素热容参数"
        onclick="search()" />
      </div></td>
    </tr>
  </table>
</form>
<br />
<div id="txtHint" style="display:inline">查询结果将在此处列出 ...</div>

</body>
</html>

这就是jsp了,查询内容反给div。
然后是java脚本:
Java code
<%@ page language="java" import="java.util.*,java.sql.*" contentType="text/html; charset=gb2312"
    pageEncoding="utf-8"%>

<% 
String element= request.getParameter("element"); 
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/zhengshi2","root","zky");
  Statement sql=conn.createStatement();
  String SqlQuery="SELECT * FROM "+element;
  ResultSet rs=sql.executeQuery(SqlQuery);
response.getWriter().write("QQ");
 %>
 
  相关解决方案