我有这个 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/