-2

I was just starting to learn AJAX from W3Schools. Copy-paste their first example codes and run it on my PC. It worked on FF but failed on Chrome. Can someone kindly tell me why did it happen ?

Screenshot from my FF/Chrome

enter image description here

Source from w3schools.com

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>
Martin
  • 2,411
  • 11
  • 28
  • 30
  • A word of advice, W3Schools is **not** associated with W3C so it is not "official" as the name might lead you to believe. Whilst I don't believe it is as bad as it used to be, this is worth a read: http://www.w3fools.com/ – Jon P Jun 25 '15 at 06:29
  • @Jon P. I read ur link and it doesnt solve my problem atm. – Olin Winata Jun 25 '15 at 07:58

1 Answers1

1

You need to launch chrome with this parameter to be able to access local files

--allow-file-access-from-files

ex: c:\Browser\chrome.exe --allow-file-access-from-files

Luca
  • 1,766
  • 3
  • 27
  • 38
  • I tried this (launched chrome from cmd with those additional words), but it didn't work. The error response that appeared in console as before, now it no longer appears. But still the content doesn't change. – Olin Winata Jun 25 '15 at 07:50