DragonWar% 2016-10-14 17:18 采纳率: 0%
浏览 109

jQuery ajax无法捕获401 [重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/20149750/unauthorised-webapi-call-returning-login-page-rather-than-401" dir="ltr">Unauthorised webapi call returning login page rather than 401</a>
                            <span class="question-originals-answer-count">
                                (15 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2016-10-14 22:22:43Z" class="relativetime">3 years ago</span>.</div>
        </div>
    </aside>

I have a issue regarding exception throwing and catching on client side. This has all worked well until I introduced HTTP401. This is my base controler than overrides OnExcpetion:

protected override void OnException(ExceptionContext filterContext)
{
    var exception = HandleException.Handle(filterContext.Exception); 
    filterContext.Exception = exception;

    if(exception is AuthenticationException)
        filterContext.HttpContext.Response.StatusCode = 401;
    else
        filterContext.HttpContext.Response.StatusCode = 500;

    filterContext.ExceptionHandled = true;
    filterContext.Result = new JsonResult()
    {
        Data = new { Message = filterContext.Exception.Message },
        ContentType = "json"             
    };
}

This is my AJAX call along with succes and error.

$.ajax(
{
    type: 'POST',
    url: loginRoute,
    data: formData,
    dataType: 'json',
    processData: false,
    contentType: false,
    statusCode: {
        401: function () {
           $('#authenticationErrorMessage').text(xhr.responseJSON.Message);
           $('#authenticationError').show();
       }
    },
    success: function (responseData) {
        $('#authenticationSucessMessage').text(responseData.Message);
        $('#authenticationSuccess').show();
        window.location.replace(responseData.Redirect);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        $('#authenticationErrorMessage').text(xhr.responseJSON.Message);
        $('#authenticationError').show();
    }
});

The problems is that neither status code nor error are called upon completion. Only success is called. If I set my StatusCode to 500 all works fine.

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33708432 2016-10-14 17:40
    关注

    You can globally handle it. May be below code help you.

    $( document ).ajaxError(function( event, jqxhr, settings, exception ) {
        if ( jqxhr.status== 401 ) {
          alert( "Triggered ajaxError handler." );
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀