What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 133 Stack Exchange communities.

This might be stupid question, but how does ajax work on SO given that the javascript files are being served by a different domain? I thought AJAX did not allow cross-domain requests.

share|improve this question
3  
Belongs on stackoverflow.com? –  Hilarious Comedy Pesto Aug 18 '09 at 17:02
    
@Pesto: Probably does, yeah, but you can see why he asked it in meta. :) –  T.J. Crowder Aug 18 '09 at 17:14

2 Answers 2

up vote 11 down vote accepted

The Same Origin Policy is based on the location of the document loading the script, not the script's location. That's why CDNs like Google's Ajax API hosting (code.google.com/apis/ajaxlibs/) and the AOL CDN work for distributing JavaScript libraries.

share|improve this answer
    
Exactly. –  Ian Elliott Aug 18 '09 at 17:17

Are you asking if JS can Request Cross-domain? If so...

It has methods of working, such as JSONP and PMXDR. But I'm fairly sure the receiving URL has to explicitly allow JSONP requests. That, or you can create your own proxy script with a technology that can stand between local javascript, and remote content.

That being said, not sure specifically how SO is doing it.

Are you asking if JS can be loaded cross-domain? If so...

Yes. Similar to images, mp3's, style-sheets, etc., Anything public really. If it's out there in the open, then you can load it. Google hosts many js-libraries in this fashion - you just load them from the central server, meaning your visitors may already have them cached.

share|improve this answer
    
All AJAX requests are still made on the site's origin. You can include JS files from any source you want. –  Ian Elliott Aug 18 '09 at 17:16
    
@Ian, I think you missunderstood me. I know you can include js from any source, but that doesn't mean you can contact any source with js. –  Jonathan Sampson Aug 18 '09 at 17:42
    
I believe the original question was referencing loading from external domains, but your answer now includes that so it doesn't matter. –  Ian Elliott Aug 18 '09 at 17:47
    
@Ian, The question is confusing, but the following lead me to provide my first answer "...I thought AJAX did not allow cross-domain requests." –  Jonathan Sampson Aug 18 '09 at 17:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .