为什么此 JavaScript 会导致 IE 中出现“权限被拒绝”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7556194/
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 does this JavaScript cause a "Permission Denied" error in IE
提问by codecowboy
The following code throws a Permission Denied
error in IE, citing jQuery (1.6.2) line 6244 Char:2:
以下代码Permission Denied
在 IE 中引发错误,引用 jQuery (1.6.2) 第 6244 行 Char:2:
function addAgreement() {
var url = window.location.toString();
var pieces = url.split('/');
var site_url = url.replace(pieces[pieces.length -1], '');
$('.login').append('<div id="dialog"></div>');
$('#dialog').load(site_url + '?page_id=443');
}
$('#dialog').dialog({
width: 800,
position: 'top',
modal: true,
buttons: {
"Agree": function() {
agreed = true;
var val = $('#registerform').attr('action') + '&agreed=1';
$('#registerform').attr('action', val);
$(this).dialog("close");
$('#registerform').trigger('submit');
},
"Disagree": function() {
agreed = false;
$(this).dialog("close");
}
}
});
It works in Firefox — is this something to do with same origin policy? jQuery is being served by Google CDN.
它适用于 Firefox — 这与同源策略有关吗?jQuery 由 Google CDN 提供服务。
UPDATEThe content being loaded is a WordPress page which also contains includes for cufon-yui.js
(served locally). I have tried serving jQuery locally too (i.e not from the Google CDN) and this made no difference.
更新正在加载的内容是一个 WordPress 页面,其中还包含用于cufon-yui.js
(在本地提供)的内容。我也尝试过在本地提供 jQuery(即不是来自 Google CDN),这没有任何区别。
UPDATE 2Removing the following script tags from the loaded page stops the error from appearing.
更新 2从加载的页面中删除以下脚本标记可阻止错误出现。
<script type='text/javascript' src='<?php echo bloginfo('template_url') ?>/inc/js/cufon-yui.js'></script>
<script type='text/javascript' src='<?php echo bloginfo('template_url') ?>/inc/js/path/to/font.js'></script>
<script type='text/javascript'>
Cufon.replace('#page')('.title');
</script>
回答by Lakshmana Kumar D
For AJAX requests, www. is seen as a sub-domain and breaks the same-origin policy for the xmlhttprequestobject. Make sure the domain in your AJAX request matches the domain of the page and your javascript file.
对于 AJAX 请求,www. 被视为子域并破坏了 xmlhttprequest 对象的同源策略。确保您的 AJAX 请求中的域与页面和您的 javascript 文件的域匹配。