2

Is it possible to avoid AJAX cross domain request limitation if JS file with $.getJSON is loaded from the same server (domain) as URL in AJAX request? Let's say I have a webservice on serverA.com which needs to be called from pages on few other domains e.g. subdomain.serverA.com, serverB.com etc. JS is placed on serverA.com and included on multiple pages on different domains with absolute URL:

<script src="http://serverA.com/ajax.js" />

while page URL is e.g. http://serverB.com/page.html

In such case, $.getJSON('http://serverA.com/service/',... will avoid cross domain limitations or not?

In other words, browsers are looking at page URL or JS source URL when evaluating same-origin policy for AJAX requests?

Aurélien Bénel
  • 3,775
  • 24
  • 45
Michlis
  • 107
  • 11
  • I think you're asking "Can I get round the cross domain limitations?". In which case the answer is "yes", with JSONP - http://stackoverflow.com/questions/5943630/basic-example-of-using-ajax-with-jsonp – Rob Baillie Dec 11 '13 at 09:12
  • did you even try to search on internet? i searched on google your question title and the second results was this: http://stackoverflow.com/questions/11299438/jquery-how-to-remove-cross-domain-limitation – giammin Dec 11 '13 at 09:13

3 Answers3

2

It sounds like you're asking "Can I get round the cross domain limitations of javascript?"

The answer is "yes", by using JSONP

There's a lot of good information here:

If that's not the question you're asking then you may need to clarify it a little.

Community
  • 1
  • 1
Rob Baillie
  • 3,436
  • 2
  • 20
  • 34
  • @Quentin nailed down my doubts. Thanks for linking good resources, proxy solution seems to be most simple to implement in my environment. – Michlis Dec 11 '13 at 09:46
1

In other words, browsers are looking at page URL or JS source URL when evaluating same-origin policy for AJAX requests?

The same origin policy is based on the URL of the HTML document the script is running on, not the URL of the script itself.

Is it possible to avoid AJAX cross domain request limitation if JS file with $.getJSON is loaded from the same server (domain) as URL in AJAX request?

Yes, but not because the JS file is loaded from there. The URL of the script is irrelevant.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

Instead of JSONP, I would recommend Cross Origin Resource Sharing.

The owner of the service, just need to add a header that gives the (static, dynamic or open) list of authorized origins.

Aurélien Bénel
  • 3,775
  • 24
  • 45