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

spring 中使用jquery实现ajax异步调用

功能:创建班级前首先检查班级是否存在

?

1.Control类

/**

* 根据班级名查询班级

* @author:qiuchen

* @createTime:2012-6-14

* @param teacherName

* @return

*/

@RequestMapping("/find")

public void findClasses(String clsName,HttpServletResponse response){

PrintWriter writer = null;

try {

writer = response.getWriter();

Classes classes = this.classesService.findByName(clsName);

if(classes == null){

writer.print("ok");

}else{

writer.print("error");

}

} catch (IOException e) {

e.printStackTrace();

} finally{

if(writer != null){

writer.flush();

writer.close();

}

}

}

?

2.JS

var flag = false;

unction check(){

var clsName = document.getElementById("name").value;

var url = "${ctx}/manage/classes/find.do?clsName="+clsName;

$.ajax({type:"POST", url:url,dataType:"text", success:function(datas) {

if(datas == "error"){

document.getElementById("error").style.display = 'inline';

document.getElementById("ok").style.display = 'none';

}else{

document.getElementById("sub").removeAttribute('disabled');

document.getElementById("ok").style.display = 'inline';

document.getElementById("error").style.display = 'none';

flag = true;

}

}});

}