javascript - 自定义错误消息未从 javascript ajax 返回自定义消息

标签 javascript c# asp.net

我有这个 javascript:

               $.ajax({
                    async: false,
                    type: "POST",
                    url: "Default.aspx/SaveTable2",
                    data: "{smallTablesTable:'" + JSON.stringify(smalltablesHot.getData()) + "',locationsTable:'" + JSON.stringify(locationsHot.getData()) + "',assetsTable:'" + JSON.stringify(assetsHot.getData()) +
                        "',costCenterBudgetsTable:'" + JSON.stringify(costCenterBudgetsHot.getData()) + "',employeesTable:'" + JSON.stringify(employeesHot.getData()) + "',laborCraftsTable:'" + JSON.stringify(laborcraftsHot.getData()) +
                        "',tasksTable:'" + JSON.stringify(tasksHot.getData()) + "',partsTable:'" + JSON.stringify(partsHot.getData()) + "',vendorsTable:'" + JSON.stringify(vendorsHot.getData()) +
                        "',stockroomPartsTable:'" + JSON.stringify(stockroompartsHot.getData()) + "',toolsTable:'" + JSON.stringify(toolsHot.getData()) + "',facilityID:'" + facID +
                        "',locSuffix:'" + document.getElementById('tbLocSuffix').value + "',ccPrefix:'" + document.getElementById('tbCCPrefix').value + "',locRemoveAfter:'" +
                        document.getElementById('cbLocRemoveSuffix').checked + "',ccRemoveAfter:'" + document.getElementById('cbCCRemovePrefix').checked +
                        "',workOrderMastersTable:'" + JSON.stringify(womsHot.getData()) +
                        "',workOrderMasterAssetsTable:'" + JSON.stringify(womAssetsHot.getData()) + "',workOrderMasterLaborTable:'" + JSON.stringify(womLaborHot.getData()) +
                        "',workOrderMasterStockroomPartsTable:'" + JSON.stringify(womStockroomPartsHot.getData()) + "',workOrderMasterToolsTable:'" + JSON.stringify(womToolsHot.getData()) + "'}",
                    contentType: "application/json",
                    dataType: "json",
                    success: function (data) {
                        var final = data.d;
                        if (final.replace("\r\n", "").replace(";", "").length == 0) {
                            final = "No data to import";
                        }
                        document.getElementById("hdResults").value = final;
                        alert("Data Imported Successfully. Check Log File.");
                        doPostBack = true;
                    },
                    error: function (request, error) {
                        var errorMsg = request.responseJSON.Message.split(';')[0];
                        if (request.responseJSON.Message.split(';').length > 1) {
                            var errorTbl = request.responseJSON.Message.split(';')[1];
                            HitTabButton(errorTbl);
                        }

                        alert("ERROR - Import Table Failed: " + errorMsg);
                    }
                });

调用此 c# 方法:

        [System.Web.Services.WebMethod]
        public static string SaveTable2(string smallTablesTable, string locationsTable, string assetsTable, string costCenterBudgetsTable,
            string employeesTable, string laborCraftsTable, string tasksTable, string partsTable, string vendorsTable,
            string stockroomPartsTable, string toolsTable, string facilityID, string locSuffix, 
            string ccPrefix, string locRemoveAfter, string ccRemoveAfter, string workOrderMastersTable, 
            string workOrderMasterAssetsTable, string workOrderMasterLaborTable,
            string workOrderMasterStockroomPartsTable, string workOrderMasterToolsTable)
        {
            throw new Exception("Required Columns cannot be blank.);
        }

我清除了很多代码。当我在 visual studio 中运行它时,一切正常,但是当我发布并将网站放在我的生产服务器上时,javascript request.responseJSON.Message 从“必填列不能为空”更改。到“处理请求时出错。我需要在 web.config 中添加什么吗?感谢任何帮助。

最佳答案

根据 ADyson 的建议,这是我想出的解决方案:

我没有创建异常,而是返回了一条消息。将 Javascript 更改为:

             $.ajax({
                    async: false,
                    type: "POST",
                    url: "Default.aspx/SaveTable2",
                    data: "{smallTablesTable:'" + JSON.stringify(smalltablesHot.getData()) + "',locationsTable:'" + JSON.stringify(locationsHot.getData()) + "',assetsTable:'" + JSON.stringify(assetsHot.getData()) +
                        "',costCenterBudgetsTable:'" + JSON.stringify(costCenterBudgetsHot.getData()) + "',employeesTable:'" + JSON.stringify(employeesHot.getData()) + "',laborCraftsTable:'" + JSON.stringify(laborcraftsHot.getData()) +
                        "',tasksTable:'" + JSON.stringify(tasksHot.getData()) + "',partsTable:'" + JSON.stringify(partsHot.getData()) + "',vendorsTable:'" + JSON.stringify(vendorsHot.getData()) +
                        "',stockroomPartsTable:'" + JSON.stringify(stockroompartsHot.getData()) + "',toolsTable:'" + JSON.stringify(toolsHot.getData()) + "',facilityID:'" + facID +
                        "',locSuffix:'" + document.getElementById('tbLocSuffix').value + "',ccPrefix:'" + document.getElementById('tbCCPrefix').value + "',locRemoveAfter:'" +
                        document.getElementById('cbLocRemoveSuffix').checked + "',ccRemoveAfter:'" + document.getElementById('cbCCRemovePrefix').checked +
                        "',workOrderMastersTable:'" + JSON.stringify(womsHot.getData()) +
                        "',workOrderMasterAssetsTable:'" + JSON.stringify(womAssetsHot.getData()) + "',workOrderMasterLaborTable:'" + JSON.stringify(womLaborHot.getData()) +
                        "',workOrderMasterStockroomPartsTable:'" + JSON.stringify(womStockroomPartsHot.getData()) + "',workOrderMasterToolsTable:'" + JSON.stringify(womToolsHot.getData()) + "'}",
                    contentType: "application/json",
                    dataType: "json",
                    success: function (data) {
                        var final = data.d;
                        if (final.startsWith("ERROR:")) {
                            doPostBack = false;
                            var finalMsg = final.replace("ERROR:", "");
                            alert("ERROR - Import Table Failed: " + finalMsg);
                        }
                        else {
                            if (final.replace("\r\n", "").replace(";", "").length == 0) {
                                final = "No data to import";
                            }
                            document.getElementById("hdResults").value = final;
                            alert("Data Imported Successfully. Check Log File.");
                            doPostBack = true;
                        }
                    },
                    error: function (request, error) {
                        var errorMsg = request.responseJSON.Message.split(';')[0];
                        if (request.responseJSON.Message.split(';').length > 1) {
                            var errorTbl = request.responseJSON.Message.split(';')[1];
                            HitTabButton(errorTbl);
                        }

                        alert("ERROR - Import Table Failed: " + errorMsg);
                    }
                });

C# 方法如下:

[System.Web.Services.WebMethod]
        public static string SaveTable2(string smallTablesTable, string locationsTable, string assetsTable, string costCenterBudgetsTable,
            string employeesTable, string laborCraftsTable, string tasksTable, string partsTable, string vendorsTable,
            string stockroomPartsTable, string toolsTable, string facilityID, string locSuffix, 
            string ccPrefix, string locRemoveAfter, string ccRemoveAfter, string workOrderMastersTable, 
            string workOrderMasterAssetsTable, string workOrderMasterLaborTable,
            string workOrderMasterStockroomPartsTable, string workOrderMasterToolsTable)
        {
            return "ERROR:Required Columns cannot be blank.";
        }

关于javascript - 自定义错误消息未从 javascript ajax 返回自定义消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57398901/

相关文章:

javascript - 如何将 <script> 放在框架集文档的 "body"中?

c# - 为什么写入文件比附加字符串更快?

c# - 在扩展中找不到 ASP.NET Core 3.0 get_HostingEnvironment() 方法

javascript - 从服务器调用 JQuery 函数 (asp.net)

javascript - 如何从 PHP 或 Javascript 调用 python 程序(具有命令行参数)?

javascript - 将 let 更改为 const 并在 for 循环中将 i++ 更改为 i+1 后出错

c# - 访问相关实体时如何获取 LINQ top(1)? .Take() 和 .FirstOrDefault() 不工作?

javascript - 无法将 iframe 从一个 div 复制到另一个 div?

asp.net - 无法使用开发服务器运行 ASP.NET Web 应用程序

javascript - 如何在学习 Javascript 的过程中取得进展