用ajax实现异步刷新,xmlHttp.responseText接到的值不对
用ajax实现异步刷新,xmlHttp.responseText接到的值不对
js:
var xmlHttp;
var json;
function createXmlHttpRequest(){
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest;
}else{
xmlHttpp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function startRequest(path,id){
createXmlHttpRequest();
switch(id){
case 1:
xmlHttp.onreadystatechange = handleStategetClasstype;
break;
case 2:
xmlHttp.onreadystatechange = handleStategetTeacher;
break;
}
xmlHttp.open("get",path);
xmlHttp.send(null);
}
function getClassinfo(url){
startRequest(url+"/teacherAction_findClasstype.action",1);
}
function getTeacher(url){
var id = document.getElementById("check_type").value();
startRequest(url+"teacherAction_findClassteacher.action",2);
}
function handleStategetClasstype(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var root = document.getElementById("check_type");
if(root.options.length==1){运动会口号
alert(xmlHttp.responseText);
json = eval('('+xmlHttp.responseText+')');
for(i=0;i<json.length;i++){
var option = new Option(json[i].classtypeid,json[i].classtypename);
root.add(option);
}
}
}
}
}
jsp中:
<script type="text/javascript" src="../js/myjs.js"></script>
<script type="text/javascript" src="../js/jquery-1.6.4.js"></script>
<!-- <script type="text/javascript">
$(document).ready(function(){
$.ajax({
url:"/teacherAction_findClasstype.action",
type:"post",
dateType:"json",
success:function(res){
json = eval('('+xmlhttp.responseText+')');
alert(json.length);
for(i=0;i<json.length;i++){
var option = new Option(json[i].classtypeid,json[i].classtypename);
alert(json[i].classtypename);
root.add(option);
}
}
})
}) -->
</script>
</HEAD>
<BODY onload="getClassinfo('<%=path %>')">
<HR align="center" >
<TABLE width="100%" class="tab_table">
<TBODY>
<TR>
<TD colspan="10" class="no_tab_main_td">
<TABLE class="lower_table" border="0" width="100%">
<TBODY>
<TR>
<TD>
<TABLE id="table1" class="raise_panel_line_table" width="100%">
<TBODY>
<TR>
<TR>
<TD width="10%" class="title_normal_td" colspan="2">班级类型:</TD>
<TD width="15%" nowrap colspan="9" class="normal_cell_td" >
<SELECT id="check_type" onchange="getTeacher('<%=path %>')">
<option value="0">===请选择===</option>
</SELECT>
</TD>
<TD width="10%" class="title_normal_td" colspan="2">教师姓名:</TD>
<TD width="65%" nowrap colspan="9" class="normal_cell_td" >
<SELECT id="check_teacher">
<OPTION value="0">===请选择===</OPTION>
</SELECT>
</TD>
<TD class="title_normal_td" align="right" colspan="">
<div align="right">
<INPUT type="button" id="btn_classinfo" class="default_button" value="检索" onClick="">
</div>
</TD>
</TBODY>
</TABLE>
TeacherAction:
public String findClasstype() throws IOException{
List<Classtype> list = service.getClasstype();
String json = JsonUtils.listToJson(list);
System.out.println("json:"+json);
PrintWriter out = ServletActionContext.getResponse().getWriter();
out.write(json);
out.flush();
return "fail";
}
这个在后台输出
json:[{"classtypeid":1,"classtypename":"JAVA"},{"classtypeid":2,"classtypename":"C++"},{"classtypeid":3,"classtypename":".NET"},{"classtypeid":4,"classtypename":"PHP"},{"classtypeid":5,"classtypename":"QRS"},{"classtypeid":6,"classtypename":"Android"}]
json值没有问题,
应该不是jar包问题,我用的struts2里面有json-lib-2.1.jar,js中也加了json.js这个文件
肯定是你在后台有用过跳转指令,把跳转指令删除。
比如struts1的后台action把return mapping.f******这一串换成return null
不让后台有页面跳转
真是这个问题,单以前就这么写的有return"fail",没有问题啊,但是这次就出问题了