我有一个由ajax调用调用的处理程序,并且我从存储过程SQL返回错误
下面是我的ajax电话
$.ajax({
url: "Handlers/MyHandlerCall.ashx?QueCtrlType=1"
contentType: "application/json; charset=utf-8",
success: function (data) {
alert('ok')
},
error: function (e) {
alert(e.responseText) // Here i want error message from my Stored Procedure
}
})
这是我的MyHandlerCall.ashx
try
{
DataTable dt = //Database Call and return DataTable which is working fine
if (dt.Rows.Count > 0)
{
context.Response.Write('ok');
}
}
catch (Exception ex)
{
context.Response.StatusCode = 500;
context.Response.Write(ex.Message); // Passing error which works locally very fine but not on my hosted environment
}
我的SQL SP返回错误
IF (1)
BEGIN
RAISERROR('Invalid Call',16,1)
RETURN
END
它在本地返回正确的错误消息,但在托管环境中则抛出内部服务器错误,而不是我的Sp消息
最佳答案
问题是您的服务器显示默认错误消息
如果要显示自定义消息,请编写以下代码
context.Response.StatusCode = 500;
context.Response.TrySkipIisCustomErrors = true;
context.Response.Write('Message');
对于IIS7 +