使用Ajax调用脚本时处理VBScript错误
我有一个使用jQueryAjax函数调用经典ASP/VB脚本的页面,它允许为成功和错误结果指定处理程序。这些处理程序接收来自ASP脚本的响应,ASP脚本只执行一些SQL代码将记录插入数据库。如果存在指示重复密钥冲突的SQL错误,我希望使用下面的代码将生成的错误消息替换为友好的错误消息。正如我所发现的,这不起作用,因为代码从未到达“if conn.Errors.Count”行。如果SQL生成错误,代码将立即返回错误消息,其中包括“conn.Execute”行的行号。有没有办法让它达到我的目的使用Ajax调用脚本时处理VBScript错误,ajax,vbscript,asp-classic,Ajax,Vbscript,Asp Classic,我有一个使用jQueryAjax函数调用经典ASP/VB脚本的页面,它允许为成功和错误结果指定处理程序。这些处理程序接收来自ASP脚本的响应,ASP脚本只执行一些SQL代码将记录插入数据库。如果存在指示重复密钥冲突的SQL错误,我希望使用下面的代码将生成的错误消息替换为友好的错误消息。正如我所发现的,这不起作用,因为代码从未到达“if conn.Errors.Count”行。如果SQL生成错误,代码将立即返回错误消息,其中包括“conn.Execute”行的行号。有没有办法让它达到我的目的 se
set conn=CreateObject("ADODB.Connection")
conn.Open ConnString
conn.Execute "INSERT ... " (long statement omitted for readability)
if conn.Errors.Count> 0 then
if instr(conn.Errors(0).Description, "duplicate key") > 0 then
Response.Write "Unable to add herb - code already exists"
else
Response.Write conn.Errors(0).Description
end if
else ' success
Response.Write "Herb added"
end if
conn.Close
set conn=nothing
正如其他人所指出的,最好的解决方案是使用“下一步错误恢复”。因此,您的代码看起来像:
on error resume next
set conn=CreateObject("ADODB.Connection")
conn.Open ConnString
conn.Execute "INSERT ... " (long statement omitted for readability)
if Err.Number > 0 then
if instr(Err.Description, "duplicate key") > 0 then
Response.Write "Unable to add herb - code already exists"
else
Response.Write conn.Errors(0).Description
end if
else ' success
Response.Write "Herb added"
end if
conn.Close
set conn=nothing
on error goto 0 '-- this will remove error handling for the rest of the page and can be considered optional in this case
请参阅:Hi-so classic ASP的可能副本的工作原理是,除非您另外指定,否则任何错误都是意外的,当它看到错误时就会退出。在您的例子中,SQL会导致错误-因此退出。您可能听说过将try-catch作为讨论错误处理的一种方式。经典ASP没有try-catch,而是有on-error resume next等。请使用其他人提供的链接。谢谢,我找到了答案。有一段时间没有做过经典的ASP。错误处理肯定很糟糕!我的大部分工作是使用Delphi进行Windows桌面开发,Delphi具有try/except。