使用jquery的ajax方法请求action类的方法

1 篇文章 0 订阅
1 篇文章 0 订阅

记录:使用jquery的ajax方法请求action类的方法,方法return void,

将需要返回的数据用response.getWriter().write("")返回。

js代码:

<script type="text/javascript">  
	var tmp = 60;
	var interval;//声明定时器  
	$(document).ready(function() {
		$("#get_yzm").click(function() {
			alert("!!");
			$.ajax({
				type : "post",
				url : "getYZM.action",
				success : function(data) {
					if (data == null) {
						alert("生成验证码失败...");
					} else {
						$("#yzm").val(data);
						$("#get_yzm").attr("disabled", "disabled");
						$("#get_yzm").html(tmp);
						interval = setInterval(chat, "1000");//定时器,一秒触发一次
					}
				}
			});
		});
	});
	function chat() {
		tmp--;
		$("#get_yzm").html(tmp);
		if (tmp == 0) {
			clearTimeout(interval);
			$("#get_yzm").attr("disabled", false);
			$("#get_yzm").html("获取验证码");
			tmp = 60;
		}
	}
</script>

 
 

jsp代码:

<button id="get_yzm" class="i-button-yzm" type="button" >获取验证码</button>  
<s:textfield cssClass="itxt" name="yzm" id="yzm" ></s:textfield>


struct.xml中的代码:不需要result

		<!-- 验证码 -->
		<action name="getYZM" class="com.skyarm.gsgl.action.ViolationsQueryAction" method="getYzm"> 

		</action>
action类中的代码:其中AbsAction继续了ActionSupport

public class ViolationsQueryAction extends AbsAction {
	public static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(ViolationsQueryActi<span style="white-space: pre;">	</span>on.class);

	private static final long serialVersionUID = 1L;

	@Autowired
	private TourService tourSErvice;

	
	/**
	 * 获取验证码
	 */
	public void getYzm(){
		HttpServletResponse  response=null;
		try {	
			HttpServletRequest request = getRequest();
			HttpSession session = request.getSession();
			moblie = (String) session.getAttribute("mobile");
			String yzm=tourSErvice.generateYzm(moblie);
			response=this.getResponse();
			response.getWriter().write(yzm);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
	}
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值