jquery Ajax错误函数未被触发

时间:2013-09-17 10:58:37

标签: c# javascript jquery ajax

下面是我的函数(第一个代码片段),当我运行下面的函数时,它通过我的处理程序进入我的代码并在我的代码中抛出异常(第二个代码片段),在异常被捕获之后,它会回到我的处理程序,并获取在catch区域内,最后转回javascript成功函数,但错误它不运行错误部分。

    BenefitOperations.performBenefitOperation = function(data) {
      $.ajax({
        type: "POST",
        url: "BenefitOperation.axd",
        data: JSON.stringify(data.BenefitOperationJson),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        beforeSend: function() { PageMask.show(); },
        success: function(response) {
          if (response.Success == true)
            performPostBack();
          else
            window.alert(Res.BenefitOperationFailure);
    },
    error: function(e, x, y) { window.alert(Res.BenefitOperationError + y); }   }); }

这是我的功能

    else
    {
        throw new ApplicationException(string.Format("Benefit operation type {0} for benefit type {1} is not registered", Enum.GetName(typeof(EmployeeBenefitData.BenefitOperationType), parameters.OperationTypeID), Enum.GetName(typeof(EmployeeBenefitData.BenefitTypeEnum), parameters.BenefitTypeID)));
    }

这是我的经纪人的抓手

    catch
    {

        jsonOutput = JsonConvert.SerializeObject(new
        {
            Success = false
        });
    }
    finally
    {
        context.Response.Clear();
        context.Response.ContentEncoding = Encoding.UTF8;
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonOutput);
    }

2 个答案:

答案 0 :(得分:5)

error的{​​{1}}回调未在您的代码中调用,因为没有错误。 $.ajax()表示检索响应时出现问题,例如。来自服务器的500错误。

在您的代码中,您自己捕获error并返回JSON。如果您更喜欢使用ApplicationException处理程序,请引发异常并且不要在C#代码中捕获它 - 但是应该注意您当前的代码是更好的方法。

答案 1 :(得分:0)

罗里是对的, 只需在遇到错误/异常或操作错误时设置HTTP 500状态代码。

您可以设置HTTP 500状态代码,如...

context.Response.StatusCode = 200;