javascript IE9 中的新 XMLHttpRequest 导致 JScript 运行时错误:对象不支持此属性或方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8559747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
new XMLHttpRequest in IE9 causes a JScript runtime error: Object doesn't support this property or method
提问by danfromisrael
happens only in IE9 (in firefox it works fine) tried lowering the security settings to minimum. i'm not sure about other versions of IE because i have 9 installed.
仅在 IE9 中发生(在 Firefox 中它工作正常)尝试将安全设置降低到最低限度。我不确定其他版本的 IE,因为我安装了 9。
Environment: asp.net 3.5 webforms scripting frameworks: Anthem.NET, jquery
环境:asp.net 3.5 webforms 脚本框架:Anthem.NET、jquery
the anthem script is trying to create an instance of XMLHttpRequest and fails i tried just create it myself on the page and had the same error. on the same project i've created a new html page and it worked fine.
国歌脚本正在尝试创建 XMLHttpRequest 的实例但失败了我尝试在页面上自己创建它并出现相同的错误。在同一个项目中,我创建了一个新的 html 页面并且运行良好。
so it might be some scripting collusion...
所以这可能是一些脚本勾结......
Anyone?
任何人?
Here is the original code that fails (on line 3) taken from the Anthem.NET framework that runs on the system:
这是从系统上运行的 Anthem.NET 框架中获取的失败的原始代码(第 3 行):
function Anthem_GetXMLHttpRequest() {
if (window.XMLHttpRequest) { // <-- This passes as True! window.XMLHttpRequest is {...}
return new XMLHttpRequest(); // <---- Fails here
} else {
if (window.Anthem_XMLHttpRequestProgID) {
return new ActiveXObject(window.Anthem_XMLHttpRequestProgID);
} else {
var progIDs = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
for (var i = 0; i < progIDs.length; ++i) {
var progID = progIDs[i];
try {
var x = new ActiveXObject(progID);
window.Anthem_XMLHttpRequestProgID = progID;
return x;
} catch (e) {
}
}
}
}
return null;
}
}
here's the Window.XMLHttpRequest value:
这是 Window.XMLHttpRequest 值:
and here is a picture of the failure that i created myself:
这是我自己创造的失败的图片:
Update:Just found out that it works in Compatibility Mode ! and when i go back to normal mode it works again! BTW: the document mode is on Quirks mode (which is the default)
更新:刚刚发现它在兼容模式下工作!当我回到正常模式时,它又可以工作了!顺便说一句:文档模式处于 Quirks 模式(这是默认设置)
采纳答案by Ryan Daniels
Recently I ran into the problem and I found that if I place the code which creates the XMLHttpRequest inside of a function that runs once the window loads that it works fine every time. Try placing your code in this:
最近我遇到了这个问题,我发现如果我将创建 XMLHttpRequest 的代码放在一个函数中,该函数在窗口加载后运行,每次都能正常工作。尝试将您的代码放在:
window.onload = function() {
var lala = new XMLHttpRequest();
}
Or if you use jquery:
或者,如果您使用 jquery:
$(function() {
var lala = new XMLHttpRequest();
});
回答by Hank
Believe it or not, ie9 DOES NOTactually support XMLHttpRequest (you can change some settings in your internet options as the end-user, but I'm sure that's not gonna be an acceptable answer to your boss).
信不信由你,ie9实际上并不支持 XMLHttpRequest(作为最终用户,您可以更改 Internet 选项中的某些设置,但我确信这不会成为您老板可接受的答案)。
For ie9, you gotta do this:
对于 ie9,你必须这样做:
var xmlhttp;
if (ie9)
{
xmlhttp=new XDomainRequest();
}
else
{
xmlhttp=new XMLHttpRequest();
}
Then, where you'll normally apply (for regular browsers):
然后,您通常会在哪里申请(对于常规浏览器):
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
// do your thing
}
};
You'll have to do this instead (for ie9):
你必须这样做(对于 ie9):
if (this.ie9)
{
xmlhttp.onload = function()
{
// do your ie9 thing
};
}
回答by FK82
In IE (< v.9 )window#XMLHttpRequest
is undefined
as this is a browser specific global Object
(i.e. for W3C conformant browsers such as the Mozilla or Webkit based browsers as well as Opera).
在 IE (< v.9 )window#XMLHttpRequest
中undefined
,这是一个特定Object
于浏览器的全局(即对于符合 W3C 的浏览器,例如基于 Mozilla 或 Webkit 的浏览器以及 Opera)。
I don't really understand?myself why
我真的不明白?我自己为什么
if(window.XMLHttpRequest)
does not evaluate to false
but well nothing you can do there.
不评估,false
但你在那里无能为力。
What you can do however is add a code fork before that to check for window#ActiveXObject
(i.e. Internet Explorer)
但是,您可以做的是在检查之前添加一个代码叉window#ActiveXObject
(即 Internet Explorer)
if(!! window.ActiveXObject) { // same as typeof window.ActiveXObject !== "undefined"
/* use MSXML */
}
else if(!! window.XMLHttpRequest) {
/* use XMLHttpRequest */
}
else throw Error("Browser does not support XHR.") ;
If you cannot modify the source code at this point and the problem persists you might want want to change frameworks.
如果此时您无法修改源代码并且问题仍然存在,您可能需要更改框架。
Edit: I just now noticed that you were saying the problem shows up in IE 9. That should actually not be the case as IE 9 supports the XMLHttpRequest
Object
.
编辑:我刚刚注意到您说问题出现在 IE 9 中。实际上不应该是这种情况,因为 IE 9 支持XMLHttpRequest
Object
.