开发者

jQuery $.get loading relative data but not executing the callback

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 th开发者_StackOverflow社区e 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?


Did you try the $.getJSON call ??

$.getJSON('config.json', null, function(data) { alert(data);  }); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜