在AJAX(XMLHtt prequest)是它更好地使用事件监听或readyStateChange? [英] in AJAX (XMLHttpRequest) is it better to use eventListener or readyStateChange?
问题描述
我再次观看,从WWDC12视频与HTML5高级效果,发现了他们使用的演示 req.addEventListener(负荷,回调,真正的)
而不是通常的的onreadystatechange
。
I was re-watching a video from WWDC12 on advanced effects with HTML5 and noticed that for the demo they used req.addEventListener("load",callback,true)
rather than the usual onreadystatechange
.
什么是加载事件和状态= 4状态= 200
情况之间的差异?
它是在相同的负载情况下被解雇或两个不同的人?
What are the differences between the load event and state=4 status=200
situation?
Is it the same load event being fired or two different ones?
推荐答案
在负荷
事件仅表明该请求是的网络的成功,不一定一个HTTP成功。一个Ajax请求,往往不是火负荷
事件或错误
事件,表示网络交易的成功或失败(作为进步活动规格):
The load
event only indicates that the request was a network success, not necessarily an HTTP success. An Ajax request with always either fire a load
event or an error
event, indicating the success or failure of the network transaction (as part of the Progress Events specification):
-
在
错误
事件在网络提取失败是由于服务器关闭或无法访问的域(即,该请求被阻止的same-来源政策)。
The
error
event fires when the network fetch fails due to the server being down or on an inaccessible domain (i.e., the request is blocked by the same-origin policy).
另外,负荷
火灾,无论HTTP code返回。
Otherwise, load
fires, regardless of the HTTP code returned.
在负荷
或错误
事件始终闪光最后,在最后一个 readstatechange
事件已解雇了,所以你可以肯定的是,负荷
或错误
回调与<运行code> xhr.readyState == 4 和 xhr.status
设置为正确的HTTP响应code。
The load
or error
event always fires last, after the last readstatechange
event has fired, so you can be sure that the load
or error
callback is running with xhr.readyState == 4
and xhr.status
set to the correct HTTP response code.
这篇关于在AJAX(XMLHtt prequest)是它更好地使用事件监听或readyStateChange?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!