Ajax调用总是从mvc动作返回错误,即使它工作正常

时间:2013-10-24 06:39:16

标签: jquery ajax asp.net-mvc asp.net-mvc-4 jquery-select2

我不能为我的生活弄清楚为什么这个ajax调用会一直返回错误。 (我是网络世界的新手,所以我猜我在这里错过了一些简单的东西:S)

在我的cshtml中,我有一个包含标签的Select2多值选择框。然后,当我尝试通过向我的控制器执行ajax调用来删除标记时,即使我的操作成功完成,它也会返回错误:

  $(function () {
        $('#tagSelector').select2({
            placeholder: 'Select a tag...',
            multiple: true,
            ajax: {
                url: '@Url.Action("SearchTags", "UnitDetails")',
                dataType: 'json',
                data: function (term, page) {
                    return {
                        searchTerm: term
                    };
                },
                results: function (data, page) {
                    return { results: data };
                }
            },
            createSearchChoice: function (term) {
                return {id: term, text: term};
            }
        }).on("removed", function(e) {
            var url = '@Url.Content("~/UnitDetails/UnTagUnit/" + Model.ViewUnitContract.Id)';
            var id = e.val;
            var tagName = e.choice.text;
            console.log(id + " : " + tagName);

            $.ajax({
                url: url,
                data: { selectedItem: tagName },
                type: 'GET',
                dataType: 'json',
                success: function() {
                    toastr.options = {
                        "closeButton": true,
                        "debug": false,
                        "positionClass": "toast-top-right",
                        "onclick": null,
                        "showDuration": "300",
                        "hideDuration": "1000",
                        "timeOut": "3000",
                        "extendedTimeOut": "1000",
                        "showEasing": "swing",
                        "hideEasing": "linear",
                        "showMethod": "fadeIn",
                        "hideMethod": "fadeOut"
                    };
                    toastr.success("Tag deleted from unit.", "Success!");
                },
                error: function() {
                    toastr.options = {
                        "closeButton": true,
                        "debug": false,
                        "positionClass": "toast-top-right",
                        "onclick": null,
                        "showDuration": "300",
                        "hideDuration": "1000",
                        "timeOut": "3000",
                        "extendedTimeOut": "1000",
                        "showEasing": "swing",
                        "hideEasing": "linear",
                        "showMethod": "fadeIn",
                        "hideMethod": "fadeOut"
                    };
                    toastr.error("Could not delete tag from unit.", "Oops!");
                }
            });
        });

在我的控制器中,UnTagUnit(int id, string selectedItem)看起来像这样:

[HttpGet]
public JsonResult UnTagUnit(int id, string selectedItem)
{
    try
    {
        UnitClient.UnTagUnit(id, selectedItem);

        // what to return if success?
    }
    catch (Exception e)
    {
        // Log and handle
        // what to return if error?
    }
}

如上所述,该方法运行正常,我的意思是成功完成UnTagUnit(id, selectedItem)部分(wcf服务)。

所以,我想我要么缺少某些东西,要么做一些根本错误的事情。还尝试了不同的方法,即返回new Json(new { success = true} )

1 个答案:

答案 0 :(得分:3)

您需要在操作中使用return JsonRequestBehavior.AllowGet,因为您使用的是GET方法,

[HttpGet]
public JsonResult UnTagUnit(int id, string selectedItem)
{
    try
    {
        UnitClient.UnTagUnit(id, selectedItem);

        // what to return if success?
        return   Json(data, JsonRequestBehavior.AllowGet)
    }
    catch (Exception e)
    {
        // Log and handle
        // what to return if error?
        //you can throw error here .. 
        throw ("Un tag failed "+e.Message);
       ///or return it as a message
         return   Json( new { success = false,message="Un tag failed "+e.Message} , JsonRequestBehavior.AllowGet)
    }
}