这个简短的 XMLHttpRequest 声明有什么问题吗?
在我检查过的所有 AJAX 库中,XMLHttpRequest 都涉及带有测试或 try/catch 语句的冗长声明。
我需要通过 SOAP GET 请求检索 XML,并且我在 IE7+、Firefox 和 Chrome 中成功测试了以下声明:
var xhr=new XMLHttpRequest()||new ActiveXObject("Microsoft.XMLHTTP");
我在这里缺少什么?我是否忽略了一些我的声明将被破坏的极端情况?
编辑
因此声明的第二部分永远不会运行。这是否意味着对于 IE7+/Firefox/Chrome 我只需要:
var xhr=new XMLHttpRequest();
In all the AJAX libraries I've checked out, XMLHttpRequest involves a lengthy declaration with tests or try/catch statements.
I need to retrieve XML via a SOAP GET request, and I tested the following declaration successfully in IE7+, Firefox and Chrome:
var xhr=new XMLHttpRequest()||new ActiveXObject("Microsoft.XMLHTTP");
What am I missing here? Did I overlook some edge cases where my declaration is going to break?
Edit
So the second part of the declaration never runs. Does it means that for IE7+/Firefox/Chrome I just need:
var xhr=new XMLHttpRequest();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你会错过 Internet Explorer 版本低于 7 的情况:
我认为有一个函数
createRequest()
例如,你不必修改并根据浏览器返回好的对象,并且包含try/catch 来处理错误将是更好的方法。You gonna miss the case for Internet Explorer versions inferior to 7:
I think having a function
createRequest()
for example that you don't have to modify and returns the good object depending on the browser, and containing try/catch to handle errors would be a better way to go.new
具有优先权,因此您正在执行new XMLHttpRequest()
并检查返回值。但是,如果未定义XMLHttpRequest
,则此执行已引发错误。相反,您想在应用new
之前检查XMLHttpRequest
是否已定义,例如:new
has precendence, so you're executingnew XMLHttpRequest()
and check the return value. However, in caseXMLHttpRequest
is not defined, this execution already throws an error. Rather, you want to check whetherXMLHttpRequest
is defined before applyingnew
, e.g.: