在使用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+"\"}";
其中\"表示转义,就表示为引号。