Take the 2-minute tour ×

I'd like to get some clarification on what cross-domain AJAX means in terms of the mechanics behind it.

Say for example, I have a website http://www.example.com. This website contains a javascript file, which within contains several standard jQuery based AJAX calls (e.g. $.post(), $.get() etc), located at http://www.example.com/js/script.js.

Now, I have another website http://www.helloworld.com, which contains the following;

<script type="text/javascript" src="http://www.example.com/js/script.js"></script>

Would the AJAX requests within http://www.example.com/js/script.js which make requests to http://www.example.com be considered "cross-domain" and therefore carry compatibility issues when the file is included on http://www.internet.com?

Any answers would be great!

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

JS is executed on the client side, so it doesn't matter where the source of the file resides, it'll be executed from the domain in the address bar.

share|improve this answer
Ah I see. This is a bit OT, and not directly part of the question, but from what I understand, AJAX requests can be made to another domain in the form of say a HTTP GET/POST based request, but any data returned must be in JSONP format? –  Avicinnian Jan 4 '12 at 0:39
It's a bit more complicated than that. You also need to append a callback to your request URL which matches the name of the remote jsonp callback. en.wikipedia.org/wiki/JSONP –  AlienWebguy Jan 4 '12 at 18:55
add comment

In order to be able to include a javascript from another domain, the sever that serves that JS need to provide the file with the correct headers. In particular the headers need to have the Access-Control-Allow-Origin set for the domain requiring a cross-domain JS.

Just for test purpose you might want to run Chrome with the parameter --allow-file-access-from-files and it won't stop cross origin requests.

share|improve this answer
add comment

Your Answer


