Javascript 错误请求上的ajax后期刷新页面

Javascript 错误请求上的ajax后期刷新页面,javascript,c#,jquery,ajax,asp.net-mvc-5,Javascript,C#,Jquery,Ajax,Asp.net Mvc 5,我有一篇类似于以下内容的ajax帖子: $.ajax({ type: "post", dataType: "html", url: "/MyController/MyAction", data: AddAntiForgeryToken({ SomeValue: "Abc" }, "#FormIdFromWhereToCopyAntiForgeryTokenFrom"), success: function (response) {

我有一篇类似于以下内容的ajax帖子:

$.ajax({
    type: "post",
    dataType: "html",
    url: "/MyController/MyAction",
    data: AddAntiForgeryToken({
        SomeValue: "Abc"
    }, "#FormIdFromWhereToCopyAntiForgeryTokenFrom"),
    success: function (response) {
        if (response === "True") {
            //do something
        } else {
            //call the function to try again in 10 seconds.
        }
    },
    error: function (e) {
        //call the function to try again in 10 seconds.
    }
});
else {
    //call the function to try again in 10 seconds.

    settimeout(function(){
       location.reload();
    }, 10000);
}
在某些情况下,这个函数可能会返回一个错误,这是非常罕见的情况,但这是可能的,我想处理它们。就这个问题而言,我认为深入探讨这些问题并不重要。在任何情况下,在我的服务器端,当该场景发生错误时,我都会设置一个检查。我捕获它,然后返回一个响应,通常会告诉浏览器刷新自己。但是,由于这是一个ajax调用,所以它不会这样做。以下是代码:

public class CustomExceptionHandler : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        var exception = filterContext.Exception;
        if (exception != null)
        {
            var exceptionType = exception.GetType();
            var someType= typeof(SomeExceptionType);

            if (exceptionType.IsInstanceOfType(someType) || someType == exceptionType)
            {
                filterContext.ExceptionHandled = true;
                filterContext.Result = new RedirectResult(filterContext.HttpContext.Request.RawUrl);
                return;
            }
        }
    }
}
正如您在上面看到的,这个想法是告诉页面刷新自己,然后问题就会得到解决。但是,我不确定如何在错误部分检查我的javascript,服务器是否返回了重定向结果,以便我可以告诉浏览器刷新


我会看页眉还是别的什么?你能举一个我如何解决这个问题的例子吗?

你可以简单地设置刷新页面的超时时间,如下所示:

$.ajax({
    type: "post",
    dataType: "html",
    url: "/MyController/MyAction",
    data: AddAntiForgeryToken({
        SomeValue: "Abc"
    }, "#FormIdFromWhereToCopyAntiForgeryTokenFrom"),
    success: function (response) {
        if (response === "True") {
            //do something
        } else {
            //call the function to try again in 10 seconds.
        }
    },
    error: function (e) {
        //call the function to try again in 10 seconds.
    }
});
else {
    //call the function to try again in 10 seconds.

    settimeout(function(){
       location.reload();
    }, 10000);
}

还是我理解错了这个问题?

Ajax有一个可以使用的
statusCode
,请参见(向下滚动或搜索
statusCode
)。对我来说,在Ajax请求中重定向会破坏Ajax请求的用途。话虽如此,您可以在响应中检查302的状态代码,以执行应该与RedirectResultCheck相对应的操作。通过这种方式可以轻松重试ajax请求:ajax调用无法重定向(ajax的全部目的是保持在同一页面上)