Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLHttpRequest cannot load Cross origin requests are only supported for HTTP

Tags:

xml

found an exception 101 in getting data from XML file:XMLHttpRequest cannot load file:///C:/Users/zaid/Desktop/xml/cd_catalog.xml. Cross origin requests are only supported for HTTP.

<script type="text/javascript">
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","xml/cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
  { 
  document.write("<tr><td>");
  document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
  document.write("</td><td>");
  document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
  document.write("</td></tr>");
  }
document.write("</table>");
</script>
like image 264
Zaid Iqbal Avatar asked Jun 25 '12 17:06

Zaid Iqbal


People also ask

How do I fix cross-origin requests are only supported for protocol schemes?

Just change the url to http://localhost instead of localhost . If you open the html file from local, you should create a local server to serve that html file, the simplest way is using Web Server for Chrome . That will fix the issue.

How do I fix CORS request not HTTP?

This often occurs if the URL specifies a local file, using the file:/// scheme. To fix this problem, make sure you use HTTPS URLs when issuing requests involving CORS, such as XMLHttpRequest , Fetch APIs, Web Fonts ( @font-face ), and WebGL textures, and XSL stylesheets.

How do I fix CORS error in HTML?

to fix the error, you need to enable CORS on the server. The client expects to see CORS headers sent back in order to allow the request. It might even send a preflight request to make sure that the headers are there. You can enable CORS server side for one, multiple, or all domains hitting your server.


1 Answers

You're trying to request a resource through your local machine, that's a cross reference. You need to access to this resource trough your HTTP server to get access to it.

like image 187
Carlos Avatar answered Sep 27 '22 23:09

Carlos