Jump to content

Fetch data from URL


lee2121

Recommended Posts

Hello, If i run the following code in an ASP page it loads the file (stored on local server) and outputs the text as expected;

<p id="demo"></p>

<button type="button" onclick="loadDoc()">Load Feed</button>

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "XML_TEST.xml", true);
  xhttp.send();
}
</script>


I need to run this from a URL not a local file. The URL has the exact same data and if i run the URL in a browser i get the data displayed in XML in the browser but when i change my code to fetch the data from the URL nothing happens.

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "https://test.httpapi.com/api/domains/available.xml?auth-userid=user&api-key=key&domain-name=domain&domain-name=domain2&tlds=com&tlds=co.uk", true);
  xhttp.send();
}
</script>

any ideas please how i can get this to work?

Link to comment
Share on other sites

AJAX is restricted from cross-domain requests unless you own the server you're requesting from or you can convince the owner to grant you permission.

For a server to grant you permission it has to send an Access-Control-Allow-Origin HTTP header with your domain name as a value.

 

There's a detailed explanation here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

 

I tried to connect to the URL you provided but it does not work, there are issues with it, so that's probably part of the problem as well.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...