-1

is there any JS function to get content of URL from another domain to a variable? Specifically, i need set content of this site: https://www.alza.cz/apps/externalbanner.ashx?type=1785 as a string to JS variable. I try couple of AJAX codes, .load jQuery fuction but nothing works. I'm beginning to think that it's not possible to do this with JS, could you help me with that please? At best, to directly divide this string into 3 variables where the separator is a space, but this is not necessary i can do it by myself.

Best, Petr

  • Possible duplicates: https://stackoverflow.com/questions/9089669/use-javascript-to-fetch-content-from-external-site https://stackoverflow.com/questions/2409581/can-i-load-data-from-an-external-page-via-ajax https://stackoverflow.com/questions/11383117/how-do-i-load-external-html-content-to-be-manipulated-by-this-js-code – dev101 Jan 02 '19 at 12:18
  • "I try couple of AJAX codes" — [Show them to us](http://idownvotedbecau.se/nocode/)! Provide a [mcve]! – Quentin Jan 02 '19 at 13:00
  • "but nothing works" — What does that mean? Tell us what happens and [what error messages are reported](http://idownvotedbecau.se/noexceptiondetails/). Make sure you know how to read JS error messages (i.e. how to use the Developer Tools in your browser) – Quentin Jan 02 '19 at 13:01

1 Answers1

0

The most common methods to send an ajax request from JS are:

The fetch api (quite new maybe not all browsers support it)

fetch('url')
  .then(response => response.text()) // response is text format
  .then(content => {
    /* do something here*/
    alert(content))
  }
)

And the old XMLHttpRequest class

  const xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     alert(this.responseText);
    }
  };
  xhttp.open("GET", "url", true);
  xhttp.send();

Note:

  • Sending AJAX requests on the main thread is a bad idea, it freezes the website, so you will get the data on an other thread

  • The remote server might not allow you to get its content from a different page, in this case the JavaScript console will show an error, like:

Access to XMLHttpRequest at 'https://google.com/' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

And there are some other less strict restriction like this, eg.: no fetching from http to https, all failed requests will be logged to console


To split a string into multiple parts use

const splitedArray = 'a b c'.split(' ' /* seperate by space */)
splitedArray[0] == 'a'
splitedArray[1] == 'b'
splitedArray[2] == 'c'
tomitheninja
  • 497
  • 2
  • 13
  • Thank you for your reply, if AJAX is not the best way how to do it, are there other options? – Karel Noel Jan 02 '19 at 13:47
  • I dint said ajax is bad, i said net request will block the thread like a while(true) loop, so they should run on a seperate thread, try this out: console.log('start') setTimout(() => console.log('In timout') , 0) console.log('finish') You will see an output like this: start finish in timout // console.log runs on seperate thread, not blocks main thread – tomitheninja Jan 02 '19 at 14:26