strut1,jquery ajax form表单的提交

本文探讨了使用Struts1框架进行表单验证的方法,并针对AJAX异步请求中出现的问题提供了解决方案。通过将AJAX请求设置为同步方式,确保了验证结果能够准确地返回并处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用strut1 进行form的自动验证,但是在ajax里面就不行。

 

var result;
    var options = {  
         success:function(data){//回调函数  
        	    result = data;
         },  
         error: function(XmlHttpRequest, textStatus, errorThrown){  
             alert( "Server busy. Please try again later."); 
         },
         resetForm:false,  
         async:false,
         url:"<%=ctxPath%>/booking/NewAdd.do?action=validate"
   };  
   $("#createForm").ajaxSubmit(options);  
   
   if("timepast" == result.resultJson){
    	alert("Sorry, there is less than 4 hours before the conference start time."
    			+ "\n\r" 
    			+"Please contact directly HGC Concierge to process your urgent request.");
    	return false;
    }
    if("overlap" == result.resultJson){
    	alert("Sorry, the resoures[" + result.bookingList + "] you selected have already been occupied.");
    	return false;
    }
    if("error" == result.resultJson){
    	alert("Server busy. Please try again later.If problem persists, please contact HGC ITFN.");
    	return false;
    }

 代码首先要插入引入的jquery插件

 

 

<script type="text/javascript" src="<%=ctxPath%>/common/jquery.form.js"></script>

表单的ID的对应在这句code里面

 $("#createForm").ajaxSubmit(options); 

其中Action的代码:

NewBookingForm newBookingForm = (NewBookingForm) form;

 直接可以拿到form表单的值并且封装为对象。

 

在这里一开始用了异步的方式调用,发现得不到准确的result的值,后面改成同步就OK了。

因为异步处理可能结果还没有返回,程序就继续运行被使用,所以有错误。

 

另外:java String 类型拼接字符串类型的示例

String jsonData = "{\"resultJson\":\""+resultJson+"\","+"\"bookingList\":\""+bookingListResult+"\"}";

 其中\"表示转义,就表示为引号。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值