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

ajax请求jsp无回传
html代码:
<!DOCTYPE html>
<html>
  <script>
  function display()
  {
   var div=document.getElementById("my");
   if(div.innerHTML!="")
   {
   div.innerHTML="";
   return;
   }
   var xmlHttp=new XMLHttpRequest();
   xmlHttp.open("GET","introduce.jsp",true);
   xmlHttp.onreadystatechange=function()
   {
   if(xmlHttp.readyState==4)
   {div.innnerHTML=xmlHttp.responseText;
   }
   }xmlHttp.send();
  }
  </script>
  <body>
    <div id="my" style="background-color:yellow;widht:300px;height:400px"></div>
    <input type="button" onclick="display()" value="hehe">
  </body>
</html>


jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<html>
<body>
out.print("this is a demo");
</body>
</html>

刚学jsp,为何html中没有获取到jsp中的数据,是不是jsp文件这样写是错的,大神帮我解答一下,哪里有问题,我在javascriptscript中用alert 没有弹出框
ajax jsp

------解决方案--------------------
JS 
   if(xmlHttp.readyState==4) 改成
  if(xmlHttp.readyState==4 && req.status == 200) 


先确定直接 地址栏输入 introduce.jsp 有东西
jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
this is a demo 

 
------解决方案--------------------

XMLHttpRequest需要IE7+以上并且没有禁用XMLHttp才能创建成功。同时增加status判断,看是不是出其他问题了

var xmlHttp=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("microsoft.xmlhttp");


xmlHttp.open("GET","introduce.jsp?_dc="+new Date().getTime(),true);//加时间戳防止缓存



删掉html,body标签,要不可能出问题

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>