【发布时间】:2013-11-28 02:51:34
【问题描述】:
在发布新网站前不久,我们发现计算机中的同源策略限制了对同一、源或域上的页面的访问。这是网站:http://blanc-encens.com/。
更糟糕的是,如果它不能立即运行,在重新加载后它会运行!!
您能否从理论上告诉我为什么会发生这种情况,或者如果您是被选中的少数人中的一员,在哪里不起作用,请检查问题?编辑:我现在手动将标题设置为“*”!
Chrome 控制台错误:
XMLHttpRequest cannot load http://blanc-encens.com/profile?ajax=1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.blanc-encens.com' is therefore not allowed access. profile:1 0 core.js:271
在 Chrome 中,在不工作的机器上的网络选项卡中没有 Response Headers。
我的代码:
var hyper = {
init : function() {
this.content($('.navigation a').not('.follow a'));
this.pop();
},
content : function(obj) {
obj.on('click', function(e){
var thisUrl = $(this).attr('href');
thisalt = $(this).attr('alt');
hyper.load(thisUrl);
e.preventDefault;
window.history.pushState(null, thisalt, thisUrl);
document.title = thisalt;
});
},
pop : function() {
window.onpopstate = function() {
hyper.load(location.pathname);
};
},
load : function(page) {
page = page == '/' ? '/start' : page;
$.ajax({
type: 'GET',
url: page,
data: {ajax : 1},
success: function(data) {
$('.content').empty().append(data);
}
});
}
};
hyper.init();
谢谢!!
【问题讨论】:
-
你有从
blanc-encense.com切换到www.blanc-encens.com的代码吗?就浏览器而言,它们是不同的域。 -
@Barmar 这听起来很有希望。但真正让你头疼的事情是,在重新加载后,它在任何地方都能正常工作!!!
-
脚本缓存可能吗?
-
@KevinB 这是怎么做到的?还是撤销? ;) 你的意思是将
$.ajax设置为cache: false? -
不,我指的是之前的评论,你说它现在正在工作。如果它现在可以工作并且您没有更改任何内容,那么您的 javascript 被缓存了,或者您有竞争条件。 ajax 缓存可能与它无关。
标签: ajax xmlhttprequest same-origin-policy