Firefox下使用XHR同步调用的问题

本文详细解释了Firefox浏览器中XMLHttpRequest对象同步调用的行为特点,包括send方法后的执行流程、异常处理以及在通讯错误时如何避免抛出异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Firefox的同步调用并不是在send结束后,回调onreadystatechange,而是顺序执行send后面的方法

2. 使用同步调用,在send后不能做xhr.onreadystatechange == null判断,否则会抛出Error: :{ UnnamedClass úùhöCP
³异常。但是给xhr.onreadystatechange赋值不会抛出异常

3. In the event of a communication error (such as the webserver going down), an exception will be thrown in the onreadystatechange method when attempting to access the .status variable. Make sure that you wrap your if...then statement in a try...catch. (See: bug 238559). 在通讯错误发生时(例如web服务器down掉),如果此时在onreadystatechange方法中访问status变量,就会抛出异常。所以在onreadystatechange中的if...else语句中判断status时要加上try块

function alertContents(httpRequest) { try { if (httpRequest.readyState == 4) { if (httpRequest.status == 200) { alert(httpRequest.responseText); } else { alert('There was a problem with the request.'); } } } catch( e ) { alert('Caught Exception: ' + e.description); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值