Javascript 成功覆盖其他逻辑时的AJAX重定向
在执行AJAX成功后,我似乎无法重定向到新页面(这是jquerymobile的脚本)。在下面的代码中,如果我执行重定向以外的其他函数,sessionStorage将按预期设置。执行重定向时,不会执行会话存储代码。建议Javascript 成功覆盖其他逻辑时的AJAX重定向,javascript,jquery,ajax,redirect,Javascript,Jquery,Ajax,Redirect,在执行AJAX成功后,我似乎无法重定向到新页面(这是jquerymobile的脚本)。在下面的代码中,如果我执行重定向以外的其他函数,sessionStorage将按预期设置。执行重定向时,不会执行会话存储代码。建议 $("#register").click(function () { var formData = $("#registerUser").serialize(); $.ajax({ type: "POST", url: "file
$("#register").click(function () {
var formData = $("#registerUser").serialize();
$.ajax({
type: "POST",
url: "file.php",
//dataType:'json',
cache: false,
data: formData,
//success: onSuccess,
success: function (data) {
if (data.status == 'success') {
//execute some code here
var someValue = (data.id);
sessionStorage.setItem('someValue', someValue);
} else if (data.status == 'error') $("#notification").text(data.message);
},
complete: function () {
window.location.replace('newPage.html');
}
});
return false;
});
你的成功和成功是相互矛盾的。当它完成时,两者都将执行。一个将重定向。将重定向代码移到success中,使其在执行其他操作后重定向
success: function (data) {
if (data.status == 'success') {
//execute some code here
var someValue = (data.id);
sessionStorage.setItem('someValue', someValue);
// redirect after success
window.location.replace('newPage.html');
} else if (data.status == 'error') {
$("#notification").text(data.message);
}
}
我自己解决了这个问题——您必须使用JQM重定向:
$.mobile.changePage('newPage.html')代码>
请注意,这只是Chrome中的一个问题。在Firefox中,一切都如期进行。实际上,我第一次尝试时也得到了同样的结果。页面重定向而不保存sessionStorage值(仅限chrome),如果您取消重定向,一切正常(在chrome中),对吗?是的--sessionStorage设置良好,没有重定向。原始问题中没有提到JQM。谢谢Hugo--我编辑了该问题,表明它是针对JQM的
success: function (data) {
if (data.status == 'success') {
//execute some code here
var someValue = (data.id);
sessionStorage.setItem('someValue', someValue);
// redirect after success
$.mobile.changePage('newPage.html');
} else if (data.status == 'error') {
$("#notification").text(data.message);
}
}