Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jqueryajax没有';我没赶上401_Jquery_Ajax_Asp.net Mvc - Fatal编程技术网

jqueryajax没有';我没赶上401

jqueryajax没有';我没赶上401,jquery,ajax,asp.net-mvc,Jquery,Ajax,Asp.net Mvc,我有一个关于客户端异常抛出和捕获的问题。在我介绍HTTP401之前,这一切都很好。这是我的基本控制器,而不是覆盖OneXpetion: protected override void OnException(ExceptionContext filterContext) { var exception = HandleException.Handle(filterContext.Exception); filterContext.Exception = exception;

我有一个关于客户端异常抛出和捕获的问题。在我介绍HTTP401之前,这一切都很好。这是我的基本控制器,而不是覆盖OneXpetion:

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"             
    };
}
这是我的AJAX调用以及成功与错误

$.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();
    }
});

问题是在完成时既不调用状态代码也不调用错误。只有成功才叫做成功。如果我将StatusCode设置为500,一切正常。

您可以全局处理它。下面的代码可能会帮助你

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

这不是问题!阅读问题。我还想补充一点,我正在使用OWIN Oauth。我已经找到了解决方案。它不起作用。我很好奇,所以我在OneException方法中将StatusCode设置为403,并确保在客户端捕捉到错误。我已经用Chrome和Edge测试过了(只是想看看Chrome是否有问题)。