【发布时间】:2015-11-22 23:04:42
【问题描述】:
当用户超时或会话 cookie cookie 被删除时使用 MVC 5 身份验证,用户将被重定向到登录页面。
这在整个页面加载时工作正常,但是对于我的 Ajax 调用,这会破坏客户端。重定向在身份验证页面显示时起作用,但没有响应(在标准加载轮之后)。
我很确定这是某种 JS 问题。
JS:
$(function () {
$('#dealSearchForm').on("submit", function (e) {
e.preventDefault();
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (data) {
$('#dealSearchForm').html(data);
},
complete: function (data) {
$('#searchResults').show();
}
});
});
});
主视图:
@{Html.RenderPartial("_DealSearch");}
<script src="@Url.Content("~/Scripts/DealSearch.js")"></script>
<script>
$(document).ready(function () {
@if (Model.Deals.Count > 0) {
@Html.Raw("$('#searchResults').show();");
}
});
</script>
局部视图:
@using (Html.BeginForm("DealSearch", DealManagement, FormMethod.Post, new { @id = "dealSearchForm" }))
{
/* Search filters etc */
<div class="col-xs-7">
<button id="button-search" class="btn btn-primary pull-right" type="submit" data-loading-text="Please wait...">Search</button>
</div>
<div id="searchResults">
/* Grid with results */
</div>
控制器:
public virtual ActionResult Index()
{
var model = new DealSearchModel();
return this.View(model);
}
public virtual ActionResult DealSearch(DealSearchModel model)
{
// Get deals from service, uses search criteria
model.Deals = GetDeals(model);
// Return the view
return PartialView(MVC.DealManagement.Views._DealSearch, model);
}
客户端出现错误:
如果有人有任何想法,他们将不胜感激!
【问题讨论】:
-
你还能显示服务器端(控制器)代码吗?
-
嗨,我已经添加了控制器。谢谢。
-
在 JS 中试试这个剃须刀代码:stackoverflow.com/questions/5614941/…
-
你能检查服务器端的 SessionId 或 Cookie 值吗?如果不存在重定向到登录页面
标签: ajax asp.net-mvc