1

i wanted to include common content of my html pages explicitly, using jQuery

but i am not able to do it plz help some one

XMLHttpRequest cannot load file:///C:/Users/dell/Desktop/html/header.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. jquery-1.11.2.min.js:4

XMLHttpRequest cannot load file:///C:/Users/dell/Desktop/html/footer.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

i am getting this error in my console

this is my code

<html>
<head>
<title></title>
<script src="jquery-1.11.2.min.js"></script>
<script> 
$(function(){
$("#header").load("header.html"); 
$("#footer").load("footer.html"); 
});
</script> 
</head>


<body>
<div id="header"></div>
<!--Remaining section-->
<div id="footer"></div>
</body>
</html>

please help

  • The error you're getting tells you all you need to know: the browser won't allow your page to load from that URL. If you're running your page from a local file (a "file://" url), then that's the problem. Put your code on a server and serve the header fragment from the same place. – Pointy Mar 05 '15 at 15:43
  • You can't make ajax requests if your are loading the html file directly from disk. Run a simple web server instead. If you have PHP installed than a simple way is `php -S localhost:3000`. – Felix Kling Mar 05 '15 at 15:43
  • Install Xampp, it's the easiest : https://www.apachefriends.org – Jeremy Thille Mar 05 '15 at 15:45

2 Answers2

1

You will have to startup a local server in order to be able to use AJAX requests.

pertrai1
  • 4,146
  • 11
  • 46
  • 71
1

You have to use a HTTP server such as NodeJS or Apache.

You cannot make a ajax call from one website to another. It's blocked by the browser.

If you use Chrome, you can launch it with --disable-web-security in order to avoid the error and using it even without a HTTP server

Michael
  • 3,308
  • 5
  • 24
  • 36