首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

ajax请求jsp无来传

2013-09-23 
ajax请求jsp无回传html代码:!DOCTYPE htmlhtmlscriptfunction display(){var divdocument.getEleme

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"%>


<%
out.print("this is a demo");
%>

热点排行