`
morris
  • 浏览: 176117 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

运用ajax prototype框架注意事项

    博客分类:
  • ajax
阅读更多
今天无聊,随便看了下prototype,觉得挺简单的,于是写了个简单的sample,供以后参考。

首先页面的代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<SCRIPT LANGUAGE="JavaScript">
<!--   	
	function ajaxTest(test){
      code = event.keyCode;
      if(code == 13){
            var f = document.getElementById('TheForm');
            if(f.searchhhNo.value != ''){  
	            var url = 'login.do?method=ajaxTest&userName='+test.value ;
	          	new Ajax.Request(url, { method: "get",
				    parameters:Form.serialize('theForm'),	    
					onSuccess: function(httpObj){
					    ajaxText = httpObj.responseText;
					    alert(ajaxText);
			            hhpnResult(ajaxText);     	    
					},
					onFailure: function(httpObj){
					    alert(httpObj.statusText);
					}				
			    });
		    }
      } 
  }
  
  function hhpnResult(msg){
      if(msg != null){
          var f = document.getElementById('theForm');
          f.username.value = msg;
      }else{
          alert('无法找到相关资料');
      }
  }
</SCRIPT>

<html>
	<body>
		<form name="theForm" action="login.do">
			<p align="center">
				ajax測試
			</p>
			<br>
			用戶名:
			<input type="text" name="username" onkeydown="ajaxTest(this)" />
			<br>
			密 碼:
			<input type="password" name="password">
			<br>
			<p>
				<input type="submit" value="提交" >
				<input type="reset" value="重設" >
			</p>
		</form>
	</body>
</html>


然后是关于spring的配置文件,没什么可说的!

最后特别要注意的是action的问题
public ModelAndView applyFpr(HttpServletRequest request,HttpServletResponse response,LoginFormBean formBean){

String returnResult = "";
....
//特別注意,要不然得不到responseText的数值,或者得到的是整个页面的html代码
        response.setContentType("text/html; CHARSET=utf-8");
        try {
		response.getWriter().write(returnResult);
		} catch (IOException e) {
			e.printStackTrace();
		}

}


写的不全,如果有什么不懂的自己多试试就明白了,呵呵。

我本来想把源代码传上来,可我们公司不能上传,郁闷!
4
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics