ajax 与数据库进行数据传输

在页面请求ajax对象,设置相应的参数进行提交,此例是后台进行登录代码的验证:

viewplaincopytoclipboardprint?

园区代码

*

正在处理...

园区代码

*

正在处理...

对应js

viewplaincopytoclipboardprint?

varxmlHttp=newXMLHttpRequest();

vardoc=document;

$("#login_code").blur(function(){

if(this.value.length!=0){

varurl="Garden.do?method=getGardenCode&login_code="+this.value;

xmlHttp.onreadystatechange=function(){

varisExistCode=xmlHttp.responseText;

if(xmlHttp.readyState==4){

doc.getElementById("loading").style.display="none";

if(isExistCode==1){

doc.getElementById("star").innerHTML='对不起,该园区代码已存在!';

}else{

doc.getElementById("star").innerHTML='恭喜,该园区代码可用';

}

}else{

doc.getElementById("loading").style.display="";

}

};

xmlHttp.open("POST",url,true);

xmlHttp.send(null);

varxmlHttp=newXMLHttpRequest();

vardoc=document;

$("#login_code").blur(function(){

if(this.value.length!=0){

varurl="Garden.do?method=getGardenCode&login_code="+this.value;

xmlHttp.onreadystatechange=function(){

varisExistCode=xmlHttp.responseText;

if(xmlHttp.readyState==4){

doc.getElementById("loading").style.display="none";

if(isExistCode==1){

doc.getElementById("star").innerHTML='对不起,该园区代码已存在!';

}else{

doc.getElementById("star").innerHTML='恭喜,该园区代码可用';

}

}else{

doc.getElementById("loading").style.display="";

}

};

xmlHttp.open("POST",url,true);

xmlHttp.send(null);

是jquery和script混用的~!~只要觉得方便,怎么整都行

数据请求处理的action请求返回的typeContent类型大概有四种:

1服务端需要返回一段普通文本给客户端--text/plain

2服务端需要返回一段HTML代码给客户端--text/html

3服务端需要返回一段XML代码给客户端--text/xml

4服务端需要返回一段javascript代码给客户端--text/javascript

5服务端需要返回一段json串给客户端--text/x-json

viewplaincopytoclipboardprint?

publicActionForwardgetGardenCode(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,

HttpServletResponseresponse)throwsException{

DynaBeandynaBean=(DynaBean)form;

Stringlogin_code=(String)dynaBean.get("login_code");

UserInfouserInfoByCode=newUserInfo();

userInfoByCode.setRole_type(newShort("7"));

StringisExistCode="null";

if(StringUtils.isNotBlank(login_code)){

userInfoByCode.setLogin_code(login_code);

if(null==super.getFacade().getUserInfoService().getUserInfo(userInfoByCode)){

isExistCode=String.valueOf("0");

}else{

isExistCode=String.valueOf("1");

}

}

super.render(response,isExistCode,"text/plain;charset=UTF-8");

returnnull;

}

publicActionForwardgetGardenCode(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,

HttpServletResponseresponse)throwsException{

DynaBeandynaBean=(DynaBean)form;

Stringlogin_code=(String)dynaBean.get("login_code");

UserInfouserInfoByCode=newUserInfo();

userInfoByCode.setRole_type(newShort("7"));

StringisExistCode="null";

if(StringUtils.isNotBlank(login_code)){

userInfoByCode.setLogin_code(login_code);

if(null==super.getFacade().getUserInfoService().getUserInfo(userInfoByCode)){

isExistCode=String.valueOf("0");

}else{

isExistCode=String.valueOf("1");

}

}

super.render(response,isExistCode,"text/plain;charset=UTF-8");

returnnull;

}

viewplaincopytoclipboardprint?

protectedvoidrender(HttpServletResponseresponse,Stringtext,StringcontentType){

try{

response.setContentType(contentType);

response.getWriter().write(text);

}catch(IOExceptione){

logger.error(e.getMessage(),e);

}

}

protectedvoidrender(HttpServletResponseresponse,Stringtext,StringcontentType){

try{

response.setContentType(contentType);

response.getWriter().write(text);

}catch(IOExceptione){

logger.error(e.getMessage(),e);

}

}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wxl1985/archive/2009/03/02/3950058.aspx

相关推荐