簡體   English   中英

自定義錯誤消息未從 javascript ajax 返回自定義消息

[英]Custom error message not returning custom message from javascript ajax

我有這個 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.";
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM