1

I install xampp server on localhost and put a text file named "a.txt" in the htdocs folder. Then from html file located in dekstop, i m trying to fetch data from the text file on server.

<!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");
      }
    var url="http://localhost/a.txt"
    xmlhttp.open("GET",url,false);
    xmlhttp.send(null);
     document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
    </script>
    </head>
    <body>

    <h2>AJAX</h2>
    <button type="button" onclick="loadXMLDoc()">Request data</button>
    <div id="myDiv"></div>

    </body>
    </html>

Any help will be appreciated.

  • It doesn't work.. :( – Tahmina Khanam Urmi Dec 27 '14 at 17:52
  • Are you seeing any error messages in your browser? What browser are you using? Also, the HTML file needs to be loaded from the same server as your txt file. Try putting it in your htdocs folder and loading it from http://localhost/ – Jamund Ferguson Dec 27 '14 at 18:08
  • No, i see no error messages. I use Mozilla browser.Actually I need to fetch data from outside since i have to put data from server into an application outside of server. – Tahmina Khanam Urmi Dec 27 '14 at 18:23
  • @Tahimina is your Firefox development console open? https://developer.mozilla.org/en-US/docs/Tools/Web_Console That's where you should see the errors. – Jamund Ferguson Dec 27 '14 at 18:31

1 Answers1

0

The problem is you are trying to get a file with other header type.

"No 'Access-Control-Allow-Origin' header is present on the requested resource"

Maybe, you can load that file in a hidden iframe and try to get the info ;)

Community
  • 1
  • 1
Jon Ander
  • 740
  • 1
  • 14
  • 23
  • Sorry to ask but i don't know how to add header in a text file.. :( – Tahmina Khanam Urmi Dec 27 '14 at 18:27
  • 1
    Press F12 in the browser and you can see the error. When you open a HTML file from the desktop, the request header is "file://C:/..." and it is different than the request header of the a.txt that is "http://localhost". It is not allowed for security. – Jon Ander Dec 27 '14 at 19:12