Mike
Mike

Reputation: 7704

Can an AJAX request be made to a file residing on the server a Javascript file is hosted on when included on a remote site?

Ok so my question title may have been a little confusing.

Here's my example:

www.WebsiteA.com is hosting MyFile.js at http://www.WebsiteA.com/MyFile.js. This file makes an AJAX request for http://www.WebsiteA.com/location/file

When this Javascript file is included on WebsiteB through the script tag, will the Javascript run into cross-domain issues or is it based upon where the actual Javascript file is hosted?

Hopefully you understand me ok, thanks for any responses :)

Mike

Upvotes: 0

Views: 184

Answers (3)

vol7ron
vol7ron

Reputation: 42099

It depends on if you're using a relative path or a full path.

  • If it's relative, the hosting server will look relative to the URL that is being called
  • If it's full, you can specify any domain/host

Upvotes: 0

Michael
Michael

Reputation: 20049

You can do this - this is how most javascript based analytics trackers work (Google Analytics etc)

Upvotes: 0

Daniel Vassallo
Daniel Vassallo

Reputation: 344291

The same origin policy applies to the domain of the site (ie. the URL you see in your browser's address bar). The JavaScript file can be hosted on any domain. The <script> tag is exempt from the same origin policy.

If the address bar in the browser is showing www.websiteB.com, you will bump into the same origin policy if you make a request to a file on www.websiteA.com. This is irrelevant of where the .js file is hosted. If this is the case, you may want to check out the following Stack Overflow post for a few workarounds:

Upvotes: 2

Related Questions