Javascript 为什么在调用 jQuery 时 IE7 和 IE8 给我“拒绝访问”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3470859/
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
Why is IE7 and IE8 Giving me "Access Denied" when calling jQuery?
提问by Reaction21
I am using the Google CDN to call the jQuery 1.4.2 Min File into my application. One FF, Chrome, Safari everything is working great. But for some reason, i get a "Access Denied" error for the jquery.min.js file on line 127...? I don't get it. Anyone have a clue why this is acting up in this way? I am totally clueless.
!
Screenshot
我正在使用 Google CDN 将 jQuery 1.4.2 Min File 调用到我的应用程序中。一个 FF、Chrome、Safari 一切正常。但是出于某种原因,我在第 127 行的 jquery.min.js 文件中收到“拒绝访问”错误...?我不明白。任何人都知道为什么会这样?我完全一窍不通。!
截屏
Code
代码
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
type="text/javascript"></script>
.
.
case 1:
methodName = "SavePropertyInformation";
var HasFoundProperty, PropertyType, NumberOfUnits,
PropertyAddress, PropertyCity, PropertyState,
PropertyZipCode, PropertyCounty;
HasFoundProperty = $("#foundProperty input[type='radio']:checked").val();
PropertyType = $('#<%= this.fvApp.FindControl("ddlPropertyType").ClientID %>').val();
NumberOfUnits = $('#<%= this.fvApp.FindControl("ddlNumberOfUnits").ClientID %>').val();
PropertyAddress = $('#<%= this.fvApp.FindControl("txtPropertyAddress").ClientID %>').val();
PropertyCity = $('#<%= this.fvApp.FindControl("txtPropertyCity").ClientID %>').val();
PropertyState = $('#<%= this.fvApp.FindControl("ddlPropertyState").ClientID %>').val();
PropertyZipCode = $('#<%= this.fvApp.FindControl("txtPropertyZipCode").ClientID %>').val();
GetCountyFromZipCode(PropertyZipCode);
PropertyCounty = GetCounty();
data = "{WebAccessID:'" + WebAccessID + "', HasFoundProperty:'" + HasFoundProperty + "', PropertyType:'" + PropertyType + "', NumberOfUnits: '"
+ NumberOfUnits + "', PropertyAddress: '" + PropertyAddress + "', PropertyCity:'" + PropertyCity
+ "', PropertyState:'" + PropertyState + "', PropertyZipCode:'" + PropertyZipCode + "',PropertyCounty:'"
+ PropertyCounty + "' }";
doAjaxReq(methodName, data, showSavingDialog);
break;
采纳答案by epascarello
Making a call to a sub domain is seen as a different domain because of the Same Origin policy. Make sure that you are setting document.domainto avoid access denied with the Same Origin policy.
由于同源策略,调用子域被视为不同的域。确保您正在设置document.domain以避免访问被同源策略拒绝。
To get the document.domain in sync you need to set it in two places. Add a script tag that set the domain, and you need to have an iframe on the page that sets the same thing on the other domain.
要使 document.domain 同步,您需要在两个地方设置它。添加设置域的脚本标记,并且您需要在页面上有一个 iframe,在另一个域上设置相同的内容。
The page that the Ajax call is made from "www.example.com" and is calling "ajax.example.com":
Ajax 调用的页面来自“www.example.com”并调用“ajax.example.com”:
<script type="text/javascript">
document.domain = "example.com";
</script>
<iframe src="http://ajax.example.com/domainCode.html"></iframe>
The "domainCode.html" would just contain the script tag
“domainCode.html”将只包含脚本标签
<html>
<head>
<script type="text/javascript">
document.domain = "example.com";
</script>
</head>
<body>
</body>
</html>
With that in place you should be able to talk between your sub domains.
有了这个,您应该能够在您的子域之间进行交谈。
回答by Tyler
Make sure IE doesn't have any proxy settings, auto config script, or anything like that in preferences. I see nothing wrong with your code.
确保 IE 没有任何代理设置、自动配置脚本或首选项中的任何类似内容。我看你的代码没有任何问题。
回答by Mattygabe
I believe the problem stems from a certain security feature in Internet Explorer where you are not allowed to load code from a remote server unless its considered "trusted" by the browser. From what I read, there are instances where the browser does not complain about this, and then there are other situations where it won't allow it. Not sure what the specific trigger is in your instance, but I would bet that's the root of your problem here.
我认为问题源于 Internet Explorer 中的某个安全功能,除非浏览器认为它“受信任”,否则不允许从远程服务器加载代码。从我读到的内容来看,有些情况下浏览器不会对此抱怨,还有其他情况则不允许。不确定您的实例中的特定触发器是什么,但我敢打赌这是您问题的根源。
If I were you, I would just load the jQuery locally until you have performance issues that prompt you to do otherwise. Name the file jquery-latest.js, and then as new versions of JQ appear, test them locally first, and then replace the file when you're confident it works. OR, keep the version-named file and upgrade each page piecemeal, whichever's easier for your application use.
如果我是您,我只会在本地加载 jQuery,直到您遇到性能问题提示您执行其他操作。将文件命名为 jquery-latest.js,然后当新版本的 JQ 出现时,首先在本地测试它们,然后在您确信它可以工作时替换该文件。或者,保留版本命名的文件并逐个升级每个页面,以您的应用程序使用更容易为准。
Source: http://geekswithblogs.net/TimH/archive/2006/05/17/78673.aspx
来源:http: //geekswithblogs.net/TimH/archive/2006/05/17/78673.aspx
回答by Randall Kwiatkowski
Are you sitting behind a firewall? It could be blocking you from connecting and downloading the jquery.js.
你坐在防火墙后面吗?它可能会阻止您连接和下载 jquery.js。

