Jump to content
Sign in to follow this  
lee2121

Fetch data from URL

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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...