Ajax Hacks-hack8 处理Request Object 的错误
来源:互联网 发布:js object to json 编辑:程序博客网 时间:2024/05/05 16:01
让Ajax能检测服务器的错误,并能像用户提供有用的信息。
Ajax技术的魅力在于允许JavaScript能在用户不干预的情况下与服务器连接。然而JavaScript开发者经常无法控制服务器组件(可能是一个web服务或其他软件)。尽管你的应用程序包括了你自己的服务器组件,也无法保证服务器永远运行正常或者用户遇到错误。因此,当程序出错时候,必须保证应用产业程序能恢复。
本hack跟踪错误并显示出错误信息,当Ajax程序失去服务器连接的时候。
问题, 问题...
hack捕获下列异常事件,并为应用程序推荐解决的办法:
正在连接的web应用程序或服务器组件暂时不可用。
应用程序连接的服务器挂掉了,或者URL改变了。
要连接的服务器组件错误,在连接的过程中发作了。
当requst对象调用open方法时,代码使用了不同的主机地址而不是用户下载的web页面地址。当调用open方法时,Request对象抛出一个异常。
你可以在其他地方使用本hack的异常处理方法。hack还使用了hack6种的股票计算程序代码。看一下html代码:
“http://www.w3.org/TR/1999/REC-html401–19991224/strict.dtd”>
Your total Stock Holdings
Figure 1-11. Request a stock’s price
我们感兴趣的代码是能捕获异常的部分,后台服务器关闭、错误、或者错误的URL。handleResponse函数是事件处理函数用来管理服务器响应,Request.onreadystatechange=handleResponse。接下来的代码使用try/catch/finally 来捕获并处理无效数字这一问题。
function handleResponse( ){
var statusMsg=“”;
try{
if(Request.readyState == 4){
if(Request.status == 200){
/* Check if the return value is actually a number.
If so, multiple by the number
of shares and display the result */
var stockPrice = Request.responseText;
try{
if(isNaN(stockPrice)) { throw new Error(
“The returned price is an invalid number.”;}
if(isNaN(numberOfShares)) { throw new Error(
“The share amount is an invalid number.”;}
var info = “Total stock value: $”+
calcTotal(stockPrice);
displayMsg(document.
getElementById(“msgDisplay“,info,“black”;
document.getElementById(“stPrice“.style.fontSize=”0.
9em”;
document.getElementById(“stPrice“.innerHTML =“price: ”+
stockPrice;
} catch (err) {
displayMsg(document.getElementById(“msgDisplay”,
“An error occurred: ”+
err.message,“red”;
}
} else {
//Request.status is 503 if the application isn‘t available;
//500 if the application has a bug
alert(
“A problem occurred with communicating between the ”
“XMLHttpRequest Object and the server program. ”+
“Please try again very soon”;
}
}//end outer if
} catch (err) {
alert(“It does not appear that the server ”+
“is available for this application. Please ”+
“try again very soon. //nError: ”+err.message);
}
}
下面看一下用来处理不同异常的代码。
try块将抛出内部的任何异常,而相应的catch将捕获这个异常,并解决异常。try catch是相对的。
当主机挂掉了会发生什么呢?尽管你请求的URL是正确的。这种情况下,试图访问Request.status 属性的代码会抛出一个异常,应为Request对象没有从服务器接收到响应,以及相应的状态属性。
因此,代码会弹出一个警告对话框来提示用户。
Figure 1–12 depicts what the alert window looks like after this type of error.
Figure 1-12. Uh-oh, server down
代码显示了用户的信息,以及更多异常错误的相关信息。你可以不管学些信息,不过这些对程序的修改和调试很有帮助。
代码里的错误变量是JavaScript错误对象。对象的信息属性就是一个错误信息,是由JavaScript引擎产生的string。
如果没有使用 TRy/catch/finally 机制,用户只能看到很不容易懂得由JavaScript引擎产生的错误信息。在关闭这个讨厌的错误提示框以后,用户就无法了解应用程序的更多状态信息了。
有时,服务器或主机运行正常,但是要连接的服务器组件有问题。这样的情况下,Request.status 属性的值是503(服务不可用)。因为状态属性的值不是200,代码的这一部分将捕获这个异常:
} else {
//Request.status is 503 if the application isn‘t available;
// 500 如果服务有错误
alert(
“A problem occurred with communicating between the ”
“XMLHttpRequest Object and the server program. ”+
“Please try again very soon”;
}
换句话说,用户可以在弹出窗口中看到应用程序状态信息。也可以显示服务器组件的错误。这样的情况下,响应状态码是500(内部服务错误)。此外404状态响应码表示服务器的静态或动态组件无法找到请求的URL,就是常见的令人讨厌的400错误(页面不存在或被删除)。
try/catch/finally语句只适用于1.4或更高的版本中。finally语句是可选的。
令人沮丧的错误的URL
如果用户请求的URL是错误的或已经改变了,会发生什么呢? 调用Request.open方法会抛出异常,因此必须放在try/catch/finally语句里边处理。
在犯法initReq( )里边,有相应的代码:
function httpRequest(reqType,url,asynch){
//Mozilla-based browsers
if(window.XMLHttpRequest){
Request = new XMLHttpRequest( );
} else if (window.ActiveXObject){
Request=new ActiveXObject(“Msxml2.XMLHTTP”;
if (! Request){
Request=new ActiveXObject(“Microsoft.XMLHTTP”;
}
}
//the Request could still be null if neither ActiveXObject
//initialization succeeded
if(Request){
initReq(reqType,url,asynch);
} else {
alert(“Your browser does not permit the use of all ”+
“of this application‘s features!”;
}
}
/* Initialize a Request Object that is already constructed */
function initReq(reqType,url,bool){
try{
/* Specify the function that will handle the HTTP response */
Request.onreadystatechange=handleResponse;
Request.open(reqType,url,bool);
Request.send(null);
} catch (err) {
alert(
“The application cannot contact the server at the moment.”+
“ Please try again in a few seconds.”;
}
}
另一种情况是请求的URL不是所想的。例如,用户想从http://www.myorg.com/app下载页面,但是打开的URL却是http://www.myorg.com,这样的错误也可以使用try/catch/finally 语句捕获。
<<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>- Ajax Hacks-hack8 处理Request Object 的错误
- AJAX Hacks 之HACK3. 使用独立的文件来取得http request
- AJAX Hacks 之HACK2. 使用Request对象与服务器进行数据传递
- 一次Ajax的503错误的处理
- AJAX返回object日期处理
- Ajax Hacks-hack7接收JSON格式的数据
- AJAX Hacks之Hack 4. 接收XML格式的数据
- AJAX Hacks 之HACK1. 检测浏览器的兼容性
- Ajax的错误处理机制探讨
- Ajax的错误处理机制探讨
- springMVC处理Ajax请求的一个错误
- 使用重写的$.ajax()统一错误处理
- request.GetResponse 400错误处理
- 400 Bad Request 错误处理
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- 使用VB开发Windows环境下的串行通信程序
- Ajax Hacks-Hack 10. 使用CSS文件产生格式化的信息
- *** Web 存储系统窗体:窗体注册表 (new)***
- Ajax Hacks-hack9 深入了解HTTP Response
- 用VB实现屏幕阴暗操作
- Ajax Hacks-hack8 处理Request Object 的错误
- 在VB中实现窗体的动态效果
- Ajax Hacks-hack7接收JSON格式的数据
- 利用VB控制窗口显示风格
- Ajax Hacks-Hack6 接收数字数据
- 用VB设计VCD播放器
- Ajax Hacks-Hack 5.取得普通字符串
- 在 VB 中 引 用 .dbf 及 索 引 文 件
- AJAX Hacks之Hack 4. 接收XML格式的数据