AJAX/jQuery/Javascript - Access a page in an external domain
The question is quite simple, the answer may not be. :)
How to make an AJAX request (preferably with 'jQuery'), to an external domain, ie a web address (for example) completely different from the server which is the site you requested this page.
What I want is to get a html page outside of the server, and display it on my page.
I also accept suggest开发者_JS百科ions from other way, without using AJAX, for example, to accomplish that.
Thank you, now.
If you're trying to take HTML from that domain and inject it into your page, just put it in an iframe.
If you're trying to access some sort of API, you'll want to use JSONP. Here's a good writeup of how it works: http://devlog.info/2010/03/10/cross-domain-ajax/
Note that JSONP will require some changes to server side code. If it's a popular API designed for this thing, it probably already supports it.
Perhaps this can help:
-> http://www.ajax-cross-domain.com/
Besides JSONP the other way this is commonly worked around is by setting up a "proxy" file on your server in php/python/ruby/some-server-language. Your "proxy" script will take a url optionally some parameters and perform a curl on that domain.
So a data flow example would be:
1) ajax call originates from client accessing yourdomain.com. The ajax request points to yourdomain.com/proxy.php passing the url as a post or get variable.
2) the PHP script takes the url and performs a curl, gets whatever data is returned by the call and echos or dies or returns that data in some other method.
3) Data is given to the calling ajax on yourdomain.com, you can now make use of said data.
Though, it does sound from your description that you just want an iframe :)
精彩评论