How can you load all page code through js and save it to a variable, like a string like " http://yandex.ru "?

  • 2
    Only through JS - no way. - newman 2:46 pm
  • Why do you need it? - tutankhamun pm
  • @tutankhamun AJAX request for a foreign domain. Since when did it become simple and without problems? - newman 2:53 pm
  • Without ajax read var xmlhttp = new XMLHttpRequest () there you can do GET requests. - nick_n_a
  • It turns out that through ajax, you can only make requests to the site from which this page was loaded? - Yuri

1 answer 1

In the form in which the question is asked, this is impossible.

Browser security systems allow AJAX requests to be made only to the domain from which the page with the given javascript was downloaded.

In general, crossdomain requests are possible - when data is requested from another domain. But in this case, settings should be made on this domain to allow such requests to be made.

If we talk about the issue, it is unlikely that yandex.ru allows such cross-domain requests from any sites.

More details can be found here.

  • And why if I put in the url of the getJSON method, for example, a link to another site with api, and enter it as in the documentation, then everything works out? This, too, is receiving data from another domain - Yuri
  • @Yuri because cross domain queries were allowed on that domain. The whole question is whether they are allowed on the site from which you want to download data or not. - newman
  • and I can resolve them on the domain? - Yuri