ajax 与数据库进行数据传输

  在页面请求ajax对象,设置相应的参数进行提交,此例是后台进行登录代码的验证:
  view plaincopy to clipboardprint?
  
  园区代码
  
  *
  
  正在处理...
  
  
  
  
  园区代码
  
  *
  
  正在处理...
  
  
  
  对应js
  view plaincopy to clipboardprint?
  var xmlHttp = new XMLHttpRequest();
  var doc = document;
  $("#login_code").blur(function(){
  if(this.value.length != 0){
  var url = "Garden.do?method=getGardenCode&login_code="+this. value;
  xmlHttp.onreadystatechange = function(){
  var isExistCode = 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);
  var xmlHttp = new XMLHttpRequest();
  var doc = document;
  $("#login_code").blur(function(){
  if(this.value.length != 0){
  var url = "Garden.do?method=getGardenCode&login_code="+this. value;
  xmlHttp.onreadystatechange = function(){
  var isExistCode = 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
  view plaincopy to clipboardprint?
  public ActionForward getGardenCode(ActionMapping mapping, ActionForm form, HttpServletRequest request,
  HttpServletResponse response) throws Exception {
  DynaBean dynaBean = (DynaBean) form;
  String login_code = (String) dynaBean.get("login_code");
  UserInfo userInfoByCode = new UserInfo();
  userInfoByCode.setRole_type(new Short("7"));
  String isExistCode = "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");
  return null;
  }
  public ActionForward getGardenCode(ActionMapping mapping, ActionForm form, HttpServletRequest request,
  HttpServletResponse response) throws Exception {
  DynaBean dynaBean = (DynaBean) form;
  String login_code = (String) dynaBean.get("login_code");
  UserInfo userInfoByCode = new UserInfo();
  userInfoByCode.setRole_type(new Short("7"));
  String isExistCode = "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");
  return null;
  }
  view plaincopy to clipboardprint?
  protected void render(HttpServletResponse response, String text, String contentType) {
  try {
  response.setContentType(contentType);
  response.getWriter().write(text);
  } catch (IOException e) {
  logger.error(e.getMessage(), e);
  }
  }
  protected void render(HttpServletResponse response, String text, String contentType) {
  try {
  response.setContentType(contentType);
  response.getWriter().write(text);
  } catch (IOException e) {
  logger.error(e.getMessage(), e);
  }
  }
  本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wxl1985/archive/2009/03/02/39 50058.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值