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 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?