如何使用错误函数有效地调试 mvc ajax 调用?

How can I debug mvc ajax calls effectively using the error function?

我对 MVC 很陌生,我编写了一个函数来覆盖提交按钮上的默认事件,我会根据提交表单更新部分视图。它工作得很好。然后它就停止工作了。我唯一改变的是我为日期选择器 (IE 9) 添加了一些 webshim 东西。我尝试对此发表评论,但什么也没有。我在 ajax 调用中放置了一个错误函数,果然,调用由于某种原因失败了。下面是我的 ajax 电话:

<script>
    $(document).ready(function () {
        $("#DropDownForm").on("submit", function (event) {
            //Prevent default action on the drop down submit form
            event.preventDefault();
            var form = $(this);
            var Project = $('#ProjectDropDown').val();
            var Release = $('#ReleaseDropDown').val();
            //Instead run this function - find Release and update table
            $.ajax({
                url: form.attr("action"),
                //async: false,
                method: form.attr("method"),
                data: form.serialize(),
                error: function (exception) { alert(exception + "\n" + form.serialize() + "\n" + form.attr("action") + "\n" + form.attr("method")) }
            })
            .done(function (result) {
                $("#ReleaseTableBodyForm").empty();
                $("#ReleaseTableBodyForm").html(result +
                    '<br/><div><button style=\"text-align:center\">Submit</button></div>');
            });
        });
    });
</script>

它引用的表格:

@using (Html.BeginForm("LoadRelease", "Home", FormMethod.Post, new { id = "DropDownForm", style = "" }))
{
    @*Dropdowns*@
    <select id="BusinessAreaDropDown" name="BusinessArea" onchange="javascript: FillGenericProject(); FillProject(); FillReleases();" style="width: 11em;">
        @Html.Partial(@"Dropdowns\_BusinessArea", Model.ProjectViewModels);
    </select>
    <select id="GenericProjectDropDown" name="GenericProject" onchange="javascript: FillProject(); FillReleases();" style="width: 11em;"></select>
    <select id="ProjectDropDown" name="Project" style="width: 18em;" onchange="javascript: FillReleases();"></select>
    <select id="ReleaseDropDown" name="Release" style="width: 11em;"></select>
    <button type="submit" id="GoButton" style="visibility:hidden;">Go</button>
}  

直到最近它还在非常愉快地调用控制器(HomeController.cs)

    [HttpPost]
    public ActionResult LoadRelease(string Project, string Release)
    {
        var ProjectID = _ProblemReportsDB.ProjectMaps
             .Where(r => r.Project == Project)
             .Select(r => r.ID).FirstOrDefault();

        ViewBag.Project = Project;

        var Releases = from row in _ProblemReportsDB.PlannedOpenCloses
                       where (row.Project == ProjectID && (Release == null || row.Release == Release))
                       select row;

        return PartialView("_TableBody", Releases.ToList());
    }

我只是不知道如何调试 ajax 调用。

错误调用输出如下:

[object Object]
BusinessArea=Test1&GenericProject=Test2&Project=Test3&Release=Create+New+Release
/Home/LoadRelease
post

全部正确。只是简单地失败了。有什么方法可以从 ajax 错误参数中获取更多信息吗? [object Object] 对我来说有点没用。

Is there some way I can get more information from the ajax error parameters? [object Object] is kind of useless to me.

根据 $.ajax

的文档

error Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )

Function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror".

When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event.

所以您得到的 [object] 是 jpXHR,它是基础 XMLHttpRequest 的 jquery 包装器。如果您需要状态文本和错误消息,请将附加参数添加到您的错误回调中:

error: function (xhr, textStatus, errorThrown) { 
      console.error(textStatus + "\n" + errorThrown); 
}

Javascript 在这方面不像 C#。所有参数本质上都是可选的,您还可以传递比函数签名所需更多的参数。