XMLHtt prequest OnLoad属性? [英] XMLHttpRequest onload property?

查看:135
本文介绍了XMLHtt prequest OnLoad属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢做尽可能多的code尽可能不jQuery的,所以对于Ajax请求,我一直在做沿MDN说什么东西行:

I like to do as much code as possible without jquery, so for ajax requests, I've been doing something along the lines of what the MDN said:

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);  
  }  
}  

我一直在寻找在谷歌教程制作的扩展和他们用他们的要求在onload。是在onload运行当readyState为4,状态200事件在听吗?如果不是,那是什么,当我用上面的方法它代替。

I was looking at the Google tutorial for making extensions and they used the onload of their request. Is the onload an event listening that runs when the readystate is 4 and the status 200? If not, what is it, and when do I use it instead of the above method.

推荐答案

XHR 2级跨起源实现其他活动比onreadystatechange的,这是进步的事件(指定statechange); loadstart,进度,错误,中止,负载,loadend

XHR level 2 with cross origin implements other events than onreadystatechange, which are progress events (specify the statechange); loadstart, progress, error, abort, load, loadend

您可以使用onload事件为pre-检查readystate为4,并继续检查XHR状态(200 - > 300 || 304我想像)

you may use the onload event as pre-checked readystate 4, and go on to check the XHR status (200 -> 300 || 304 i'd imagine)

这篇关于XMLHtt prequest OnLoad属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆