jQuery $.get 加载相关数据但不执行回调
以下内容并不是跨域问题,但听起来像是一个。
这是我的设置(这很快让我灰心丧气):
我有一个index.html 文件,它正在加载一个名为config.json 的平面文件,该文件位于index.html 旁边。我使用了 $(document).ready() 和 window.addEventListener('load'... 来执行此操作:
$(document).ready(function(){
$.get('config.json', function(data){
alert('asd');
})
});
警报几乎永远不会触发。这种情况发生在 Chrome 9 和 Firefox 3.6 和 FF4 中没有尝试过其他浏览器。我查看了 Firebug 和网络选项卡下的开发控制台,我可以看到 config.json 正在加载。因此数据已加载,但回调已加载。 但是,在 Firefox 中,如果我以快速双模式(如心跳)刷新,那么
我的警报很少会在 3.6 中触发,所以我假设这是因为。 js 引擎比 v4 和 Chrome 慢一点
,更奇怪的是,如果我在同一域上设置 config.json 的完整路径,则它无法加载。 href="http://localhost/config.json" rel="nofollow">http://localhost/config.json 并从 http://local.mac.com/ 我的警报会触发!如果我切换域并从本地主机访问并使用 http://local.mac.com (或 abcdef.com)用于 config.json,它也会触发。
我在 /etc/hosts (mac) 中设置了一个“local.mac.com”域,因此我可以用它而不是本地主机来测试站点。我还尝试了“abcdef.com”的 /etc/hosts 中的另一个域并得到相同的结果。
一定有什么东西我错过了,因为我的时间已经快凌晨两点了,但我很困惑。有人能告诉我到底发生了什么事吗?
The following doesn't come across as a cross-domain issue, but it smells like one.
So here's my setup (that's quickly making me grey):
I have an index.html file that is loading a flat file called config.json that resides right next to index.html. I have used both $(document).ready() and window.addEventListener('load'... to do this:
$(document).ready(function(){
$.get('config.json', function(data){
alert('asd');
})
});
The alert almost never fires. This happens in Chrome 9 and Firefox 3.6 and FF4. Haven't tried other browsers. I look in Firebug and the dev console under the network tab and I can see config.json loading. It has the proper headers (application/json). So the data is getting loaded, but the callback function doesn't fire.
However, in Firefox if I refresh in a quick-double pattern (like a heartbeat), then very occasionally my alert fires. I can only get Firefox to do that in 3.6, so I'm assuming its because the js engine is a bit slower than v4 and Chrome.
To top it off, it gets stranger. If I set a full path to config.json on the same domain, it fails to load. If I set config.path to http://localhost/config.json and access the site from http://local.mac.com/ my alert FIRES! If I switch the domains and access from localhost and use http://local.mac.com (or abcdef.com) for config.json, it ALSO FIRES.
I have a domain of "local.mac.com" setup in /etc/hosts (mac) so I can test sites with that instead of localhost. I also tried another domain in /etc/hosts of "abcdef.com" and get the same results.
There must be something that I'm just missing because its almost 2am my time, but I'm flummoxed. Can someone tell me what the hell is going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试过 $.getJSON 调用吗?
Did you try the $.getJSON call ??