AJAX / jQuery / Javascript - access to a page in an external domain

The question is quite simple, there can be no answer. :)

How to make an AJAX request (preferably with "jQuery") to an external domain, that is, a web address (for example) that is completely different from the server, which is the site you requested on this page.

I want to get an html page outside the server and display it on my page.

I also accept offers in a different way, without using, for example, AJAX, to accomplish this.

Thanks, now.

+1
source share
3 answers

If you are trying to take HTML from this domain and enter it on your page, just put it in an iframe.

If you are trying to access some API, you need to use JSONP. It says well here how it works: http://devlog.info/2010/03/10/cross-domain-ajax/

Note that JSONP will require some server-side changes. If it is a popular API designed for this purpose, it probably already supports it.

+3
source

Perhaps this may help:

-> http://www.ajax-cross-domain.com/

+1
source

Besides JSONP, another way that is commonly used is to create a "proxy" file on your server in php / python / ruby ​​/ some-server-language. Your "proxy" script will receive an optional URL with some parameters and perform a curl in this domain.

Thus, an example data stream would be:

1) an ajax call comes from a client accessing your domain. An ajax request indicates that yourdomain.com/proxy.php is passing the URL as a message or receiving a variable.

2) The PHP script takes the url and performs a curl, receives any data returned by the call, echos or dies or returns this data in some other way.

3) The data is transferred to the ajax caller on yourdomain.com, now you can use the specified data.

Although, from your description, it just sounds like you just want an iframe :)

0
source

Source: https://habr.com/ru/post/898538/


All Articles