AJAX成功重定向覆盖其他逻辑
AJAX Redirect on Success Overriding other logic
在成功执行AJAX后,我似乎无法重定向到新页面(这是在jQuery Mobile的脚本中)。在下面的代码中,如果我执行重定向以外的其他功能,sessionStorage将按预期设置。执行重定向时,不会执行sessionStorage代码。建议??
$("#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: 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');
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);
}
}
相关文章:
- 如何覆盖锚点元素's href目标,并在我点击转到目标javascript时删除其他错误
- setInterval 覆盖其他 setIntervals
- AJAX成功重定向覆盖其他逻辑
- 防止固定 Div 覆盖其他 Div
- 如何让这个可拖动的框覆盖所有其他网站元素
- DIV 覆盖/重叠其他元素
- 为什么某些原型函数覆盖有效而其他函数无效
- 如何覆盖与其他模板的共模
- 脚本不允许lightbox覆盖嵌入的Youtube视频(wmode=透明),但其他脚本可以工作
- 使用CSS悬停类悬停在链接上,并在页面的其他位置显示覆盖图像
- 想要在其他服务中覆盖创建的angularJS对象,但是在应用程序中按ctrl +f5后它会被重新初始化
- 增加标题栏和其他元素'宽度以覆盖整个JQuery对话框
- 如何覆盖现有的骨干模型方法,保持其他方法相同
- 自定义验证指令覆盖其他验证
- 在HTML/JavaScript中,如何动态设置页眉/段落元素's的文本而不覆盖其他内容
- 从后端加载的模态被其他元素覆盖
- 无法覆盖来自其他框架的JavaScript函数
- 将jQuery创建的文本输入设置为隐藏的输入值,而不覆盖所有其他属性
- Grunt并发任务覆盖其他任务的日志记录
- addEventListener覆盖其他事件操作