asp.net ajax异常处理
msdn官方示例:http://msdn.microsoft.com/zh-cn/library/bb398934%28v=VS.90%29.aspx
总的来说,
1,注册ScriptManager的AsyncPostBackError事件,并在服务器实现它,并抛出被处理过的异常(根据是否为null,可能需要抛出e.Exception或e.Exception.innerException)
2,客户端用JS来响应,Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);添加处理程序
3,实现EndRequestHandler,错误信息基本在第二个参数"args“里面,
4,此时已经完成,由于网上没搜到现成文档(有的话请留言),这个args里面到底有什么经过我的枚举,暂列如下
内容比较多,我还是先把枚举的方法写出来吧:
注:下面内容是临时写的,并没有用到递归,也就是说,可能结果还不是完整的,有兴趣的可以自己拿去改造
但我们一般用到的,基本上都在了
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler); function EndRequestHandler(sender, args) { //if (args.get_error() != undefined) {alert(args.get_error().message.replace(/.*Exception:/,'')); args.set_errorHandled(true); } if (args.get_error() != undefined) { var members = 'The \'args\' members are:\n\n'; for (var i in args) { members += '\t' + i; var ai = eval("args." + i); if (typeof ai == "function") { try { members += "()"; ai = eval("args." + i+"()"); } catch (err) { members += " not implement \n"; continue; } } members += '\n'; if (typeof ai == "string") continue; for (var t in ai) { members += '\t\t' + t; if (typeof ai == "function") var ait = eval("args." + i + "()." + t); else var ait = eval("args." + i + "." + t); if (typeof ait == "function") { try { members += "()"; ait = eval("args." + i + "." + t + "()"); } catch (err) { members += " not implement \n"; continue; } } members += "\n"; if (typeof ait == "string") continue; for (var m in ait) { members += '\t\t\t' + m + '\n'; } } } document.write("<textarea style='width:1000px; height:600px;'>"+members+"</textarea>");//members如果显示请自行处理 args.set_errorHandled(true); } }
结果:
The 'args' members are: _errorHandled _error message name httpStatusCode format localeFormat _toFormattedString stack fileName lineNumber format localeFormat _toFormattedString popStackFrame() _dataItems _response _webRequest _url _headers _body _userContext _httpVerb _executor _invokeCalled _timeout _events add_completed remove_completed completed _get_eventHandlerList get_url set_url get_headers get_httpVerb set_httpVerb get_body set_body get_userContext set_userContext get_executor set_executor get_timeout set_timeout getResolvedUrl invoke constructor _resultObject _xmlHttpRequest dispatchEvent removeEventListener onreadystatechange open readyState setRequestHeader send status responseText channel responseXML statusText abort getAllResponseHeaders getResponseHeader sendAsBinary overrideMimeType multipart mozBackgroundRequest withCredentials upload onuploadprogress onabort onerror onload onloadstart onprogress getInterface _responseAvailable _timedOut _timer _aborted _started _onReadyStateChange() _clearTimer() _onTimeout() get_timedOut() get_started() get_responseAvailable() get_aborted() executeRequest() not implement getResponseHeader() not implement getAllResponseHeaders() not implement get_responseData() not implement get_statusCode() not implement get_statusText() not implement get_xml() not implement abort() constructor() get_webRequest() _set_webRequest() get_object() not implement get_dataItems() get_error() message name httpStatusCode stack fileName lineNumber popStackFrame get_errorHandled() set_errorHandled() not implement get_response() _webRequest _resultObject _xmlHttpRequest _responseAvailable _timedOut _timer _aborted _started _onReadyStateChange _clearTimer _onTimeout get_timedOut get_started get_responseAvailable get_aborted executeRequest getResponseHeader getAllResponseHeaders get_responseData get_statusCode get_statusText get_xml abort constructor() callBaseMethod getBaseMethod getBaseType getInterfaces getName implementsInterface inheritsFrom initializeBase isImplementedBy isInstanceOfType registerClass registerInterface resolveInheritance registerEnum get_webRequest _set_webRequest get_object constructor() not implement