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

时间:2019-08-07 16:38:31

标签: 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中添加一些内容吗?感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

根据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.";
        }