先来看下实例,是一个form表单,提交相关信息后台校验是否正确,如果正确就跳转到相关的页面,如果出现错误就提示错误的信息。下面的实例在chrome浏览器中测试正常,但是到了IE或者火狐,就有很大的几率在接收到返回的正确状态后不进行跳转,开始以为是语法问题不兼容IE浏览器,IE确实不兼容xmlhttprequest对象为json的写法。将这个改了依然无法正常跳转,function accountLogin(){
var str="/ajax_checkAccount.action";
var data_str=encodeToUTF8(str);
var postData = $('#login_form').serialize();
$.ajax({
url: data_str,
type:'POST', //GET
data: postData,
async:false, //或false,是否异步
dataType:'json', //返回的数据格式:json/xml/html/script/jsonp/text
success:function(msg) {
if(msg.ret==0){
var t=msg.msg;
if(t!=null&&t!=""){
window.location.href="ok.html?"
$("#portal-login-tip").html(t);
alert(t);
}
}else if(msg.ret==1){
$("#InputAccount").focus();
$("#portal-login-tip").html("账号密码错误!!");
alert("账号密码错误!!");
}else if(msg.ret==2){
$("#InputAccount").focus();
$("#portal-login-tip").html("系统未启用帐号API接口!!");
alert("系统未启用帐号API接口!!");
return false;
}else if(msg.ret==3){
$("#InputAccount").focus();
$("#portal-login-tip").html("系统未匹配帐号API接口!!");
alert("系统未匹配帐号API接口!!");
}else{
$("#InputAccount").focus();
var t=msg.msg;
if(t==null||t==""){
$("#portal-login-tip").html("未知错误!!");
alert("未知错误!!");
}else{
$("#portal-login-tip").html(t);
alert(t);
}
}
},
error : function() {
// view("异常!");
alert("异常!");
}
})
}
掌握主流浏览器的调试模式是很有必要的,从浏览器的调试过程中发现跳转的步骤直接被挂起了,JS执行也是没有报错信息。
后面看地址栏像是form的提交格式,然后就找到真的问题了,在ajax提交过程完成后,form会发起正常的提交流程,所以导致无法进行正常的跳转。
解决方案:在ajax结束的位置增加一个参数,打断form的下一步提交,就能正常进行跳转了。
return false;