Can you use Cross-Domain Ajax through a script on the other domain?
This is similar to other questions, yet I wasn't able to see an exact match. Sorry if this has been asked before, and I wasn't able to find the answer.
I know that if you have a script on one domain (say one.com/index.php
runs one.com/script.js
), then you can't use Ajax to connect to a third party domain (say connecting to two.com/ajax.php
).
Would the same be true if you were running a script on the other domain (one.com/index.php
running two.com/connect.js
), and then the script on开发者_如何学运维 two.com
wouldn't be going to another domain?
Don't know much about Ajax (just enough to modify the basic code to work for me), so thought I should ask.
Update
Furquan's answer seems to be good, but the solution has yet to be tested (I will soon, and update again), and I was able to follow links from Rudie's post to find this page, which seems to describe how I could go about implementing a solution in PHP without having to have a script on the second domain. I thank all who answered for taking their time to do so.
No. It doesn't matter where the .js
was loaded from. It matters where the connection XHR comes from. These days with all the CDN's, scripts are all over the place, but the primary domain is (and should be) the one where the scripts run.
If you 'own' both domains, you could look into https://developer.mozilla.org/en/http_access_control
edit
Huh... I don't think I understood (or understand)... Or do I? The most important thing is: on which domain does the XHR request start and where does it go to. Not imporant is: where are the .js
files located.
Yes, you can use it like the way you mentioned in the scenario, two.com/connect.js
will be able to access two.com
without any problem. Though there can be other ways to achieve the same.
No, the location of the script doesn't matter, it's the location of the current page. Both your examples will have the same cross domain restrictions.
精彩评论