javascript - 重定向到 ASP .NET MVC 中的页面后显示通知
问题描述
下面是我的ajax代码:
$.ajax({
url: '@Url.Action("AddUser", "ControllerName")',
dataType: 'json',
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
type: 'POST',
data: {
//data
},
success: function (data, textStatus, jqXHR) {
console.log(jqXHR.status);
if (data.isSuccess) {
alert(data.message);
}
else {
alert(data.message);
}
window.location.href = "@Url.Action("Index", "ControllerName")";
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR.status);
window.location.href = "@Url.Action("Index", "ControllerName")";
}
});
而不是显示alert(data.message);
我想在页面加载后显示自定义通知。
我不想传递任何查询字符串参数,因为它在 url 中可见。
有两种可能的方式来显示通知:
1)
$(".notificationdiv").html("<div class='notification alert alert-success' role='alert'>
<strong> Operation performed successfully</strong>
</div>");
$(".notification").fadeOut(4000);
- 我有一个自定义的基本控制器虚拟方法来显示通知。在页面重定向后调用该方法。
请让我知道如何显示通知。代码示例表示赞赏。提前谢谢你。
解决方案
在 window.location.href 之前的 ajax 成功添加这个
sessionStorage.successMessage= true;
并将其添加到您的 jquery 中
$(function () {
if (sessionStorage.successMessage) {
$(".notificationdiv").html("<div class='notification alert alert-success' role='alert'><strong> Operation performed successfully</strong></div>");
$(".notification").fadeOut(4000);
sessionStorage.successMessage= false;
sessionStorage.removeItem("successMessage") //if you want remove from session storage
}
});
推荐阅读
- r - 如何解决:错误:需要“命名空间”文件
- css - 知道为什么这个缓动功能不起作用吗?
- python - 传递给 API 时 Python 字符串被欺骗
- nearprotocol - 如何在 NEAR 协议中构建和部署多个合约
- maven - Maven Shade 插件:如何解决警告消息“定义 1 个重叠资源:[WARNING] - META-INF/MANIFEST.MF”
- php - 如何将文本输入中的表情符号保存为
- arrays - 以角度获取数据数组到对象
- ios - 从需要缺少 dsym 的 Crashlytics 中删除崩溃
- php - 在 Woocommerce 档案中为可变产品设置最大显示的可用库存数量
- php - 为什么 Laravel 在加载数据时有时会使用单数数据库表名?