由于大字符串数据参数

时间:2018-05-08 09:37:23

标签: c# jquery ajax asp.net-mvc error-handling

在我的视图文件中,我正在进行ajax调用,如下所示:

 $.ajax(
    {

        url: '@Url.Action("LMTDetailDashboardList", "DetailUsage")',
        dataType: "json",
        data:
        {
            ServerID: ServerID,
            LicenseId: LicenseId,
            company: company,
            fromDate: fromDate,
            toDate: toDate,
            fromhour: fromHour,
            toHour: toHour
        },
        type: "GET",
        error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            toastr.error(err.message);
        },
        beforeSend: function () {
            $("#divLoading").show();
        },
        success: function (data) {
            LMTDetailDashboardChart(data, Zaxis);
            return false;
        },
        @*error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            toastr.error(err.message);
        },*@
            //error: function (jqxhr, status, exception) {
            //    alert('Exception:', exception);
            //},
       error: function (XMLHttpRequest, textStatus, errorThrown) {
           alert("error");
           console.log('Could not get posts, server response: ' + textStatus + ': ' + errorThrown);
            },
        complete: function () {
            $("#divLoading").hide();
            $("#exportuserlist").show();
        }
        });
    return false;

控制器代码如下:

 public JsonResult LMTDetailDashboardList(String ServerID, String LicenseId, String Company, String FromDate, String ToDate, String FromHour, String ToHour)
    {

        int nameLength = Company.Length;
        int nameLength2 = ServerID.Length;
        int nameLength3 = LicenseId.Length;
        System.Text.ASCIIEncoding.ASCII.GetByteCount(Company);



        LMTUsage objLMT = new LMTUsage();
        LMTDAL objLMTDAL = new LMTDAL();

        TempData["Company"] = Company;
        TempData["ServerID"] = ServerID;
        TempData["LicenseId"] = LicenseId;
        TempData["FromDate"] = FromDate;
        TempData["ToDate"] = ToDate;
        TempData["FromHour"] = FromHour;
        TempData["ToHour"] = ToHour;

        if (object.Equals(ServerID, null))
        {
            ServerID = "All";
        }
        try
        {
            var response = objLMTDAL.GetLMTDetailUsage(ServerID, LicenseId, Company, FromDate, ToDate, FromHour, ToHour);
            if (!object.Equals(response, null))
            {
                objLMT.LMTDetailUsageList = response.ToList();
            }
        }
        catch (Exception ex)
        {
            throw;
        }
        return Json(objLMT.LMTDetailUsageList, JsonRequestBehavior.AllowGet);
    }

当“company”值的长度/大小较小时,ajax调用会触及控制器操作,但是当它更大时,会抛出错误并且不会触及控制器方法。
我试图在很多方面纠正错误但却无法解决。使用浏览器调试器,我现在收到以下错误。 Error Screen
注意:“company”变量包含多选下拉列表中逗号分隔的值 请注意它。

1 个答案:

答案 0 :(得分:0)

问题是因为数据长度。 GET请求有长度限制。这就是为什么ajax调用没有击中控制器。您可以使用POST而不是GET方法。 POST请求对数据长度没有限制。

使用以下方式更改'type':

  

键入:“POST”,

你的最终剧本将是:

$.ajax(
    {

        url: '@Url.Action("LMTDetailDashboardList", "DetailUsage")',
        dataType: "json",
        data:
        {
            ServerID: ServerID,
            LicenseId: LicenseId,
            company: company,
            fromDate: fromDate,
            toDate: toDate,
            fromhour: fromHour,
            toHour: toHour
        },
        type: "POST",
        error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            toastr.error(err.message);
        },
        beforeSend: function () {
            $("#divLoading").show();
        },
        success: function (data) {
            LMTDetailDashboardChart(data, Zaxis);
            return false;
        },
        @*error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            toastr.error(err.message);
        },*@
            //error: function (jqxhr, status, exception) {
            //    alert('Exception:', exception);
            //},
       error: function (XMLHttpRequest, textStatus, errorThrown) {
           alert("error");
           console.log('Could not get posts, server response: ' + textStatus + ': ' + errorThrown);
            },
        complete: function () {
            $("#divLoading").hide();
            $("#exportuserlist").show();
        }
        });
    return false;

祝你好运!