Ajax调用返回错误xhr.status = 200但没有更新?

时间:2018-01-16 20:11:36

标签: jquery ajax asp.net-mvc

我有一个MVC 5项目,我正在尝试使用Ajax更新记录,重新填充一些HTML表数据,然后将其返回到页面。我的问题是,Ajax代码似乎正在下载到我的错误部分,但它将作为状态代码200返回,而不是使用部分视图更新我的页面。我的理解是HTTP代码200是“OK”,意味着没有错误。我也没有在Dev Tools控制台中看到任何错误。

当我调试代码时,我可以看到它进入Controller的ActionResult,当我逐步浏览部分视图的.cshtml页面时,我也看到了数据。因此,看起来数据正在更新记录并返回HTML表。

你可以在我的Ajax错误部分看到我正在显示状态代码为500的简单消息,否则如果有不同的错误则只显示错误代码。我计划稍后清理一下,但现在我只想查看是否有任何错误。

任何人都知道为什么我会得到200状态代码然后它会进入Ajax调用与成功部分的错误部分?

   function ApproveDeny(homeDirectory, rid, arg, shift, currentEmployeeId) {
    $.ajax({
        type: "POST",
        url: homeDirectory + "/Exception/ExceptionApproveDeny/",
        data: '{rid: "' + rid + '", arg: "' + arg + '", shift:"' + shift + '", empId:"' + currentEmployeeId + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            $("#exceptionTable").html(response);
        },
        error: function (xhr) {
            if (xhr.status == 500) {
                alert('Error Code: ' + xhr.status + '\nError: Internal Server Error.');
            }
            else {
                alert('Error Code: ' + xhr.status);
            }
        }
    });
};

这是我的Controller ActionResult

public ActionResult ExceptionApproveDeny(int rid, string arg, string shift, string empId)
        {
            logger.Info("Setting exception Approve/Deny. RID: {0} | Arg: {1} | Shift: {2}.", rid, arg, shift);

            var listExceptions = new List<EssExceptionLog>();
            try
            {
                DAL.ExceptionApproveDeny(rid.ToString(), arg, string.Empty);

                listExceptions = DAL.GetEmployeeExceptionsByDate(CurrentEmployeeId, DateTime.Parse(shift));                
            }
            catch (Exception ex)
            {
                logger.Error("Error Approve/Deny exception. RID: {0} Arg: {1} Shift: {2} | {3} | {4} | {5}", rid, arg, shift, ex.Message, ex.StackTrace);
                ModelState.AddModelError(string.Empty, string.Format("Error: {0} | {1}", ex.Message, ex.StackTrace));

            }


            //setup VM with the data needed for the partial view
            var vm = new ExceptionLogDailyViewModel();
            vm.CurrentEmployeeId = empId;
            vm.ShiftDate = DateTime.Parse(shift);
            vm.listExceptions = DAL.GetEmployeeExceptionsByDate(vm.CurrentEmployeeId, vm.ShiftDate);

            return PartialView("_DailyLogExceptionsTable", vm);
        }

更新

function ApproveDeny(homeDirectory, rid, arg, shift, currentEmployeeId) {
    alert("rid: " + rid + "\nArg: " + arg + "\nShift: " + shift + "\nEmpID: " + currentEmployeeId);

    var ajaxData = '{rid: "' + rid + '", arg: "' + arg + '", shift:"' + shift + '", empId:"' + currentEmployeeId + '"}';
    alert(ajaxData);

    $.ajax({
        type: "POST",
        url: homeDirectory + "/Exception/ExceptionApproveDeny/",
        data: '{rid: "' + rid + '", arg: "' + arg + '", shift:"' + shift + '", empId:"' + currentEmployeeId + '"}',
        success: function (response) {
            $("#exceptionTable").html(response);
        },
        error: function (xhr) {
            if (xhr.status == 500) {
                alert('Error Code: ' + xhr.status + '\nError: Internal Server Error.');
            }
            else {
                alert('Error Code: ' + xhr.status);
            }
        }
    });
};

更新2

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

经过更多的研究和反复试验,我终于找到了问题所在。问题是我如何将数据传递给控制器​​。我也不需要datatype = JSON。如果其他人正在搜索类似的问题,这里是我能够工作的Ajax调用。

   function ApproveDeny(homeDirectory, rid, arg, shift, empId) {
    $.ajax({
        type: "POST",
        url: homeDirectory + "/Exception/ExceptionApproveDeny/",
        data: { rid: rid, arg: arg, shift: shift, empId: empId },
        success: function (response) {
            $("#exceptionTable").html(response);
        },
        error: function (xhr) {
            if (xhr.status == 500) {
                alert('Error Code: ' + xhr.status + '\nError: Internal Server Error.';
            }
            else {
                alert('Error Code: ' + xhr.status);
            }
        }
    });
};