0

I have a index.html page and link to another javascript file d3.html. In d3.html javaScript file the javaScript code do some interesting stuff with a local file amazon.txt (in my computer). I code and test in local XAMPP web server and everything works fine. However, if i close the XAMPP (web server), the d3.htmlcannot be loaded. The d3.html have an error Cross origin requests are only supported for protocol schemes

In index.html:

<p><a href="d3.html">link</a></p>

in d3.html:

$.get('amazonCut.txt', function (data) {
          // interesting stuff
        }

If I close XAMPP, index.html can be loaded but d3.js cannot. I browsed the stackoverflow and changed above code to:

<p><a href="http://localhost/d3.html">link for complete result </a></p>

and

$.get('https://localhost/amazonCut.txt', function (data) {
          // interesting stuff
        }

but it won't work. The reason I am doing this because the instructor only clicks the index.html file when grading without setting any web server.

SHE
  • 23
  • 1
  • 6
  • You need a web server to load local files. If you put the content of that file (be it a txt, csv, tsv etc) as a variable in the JS code, you don't need the server. – Gerardo Furtado Jan 28 '17 at 05:26
  • You have shut down your XAMPP server and then issuing it a request? That's why you are getting the error. – Shubhranshu Jan 28 '17 at 05:27
  • how to read the file and put as a variable?@GerardoFurtado – SHE Jan 28 '17 at 05:28
  • I need to run it without XAMPP. @Shubhranshu – SHE Jan 28 '17 at 05:29
  • @SHE What's the file in question? – Gerardo Furtado Jan 28 '17 at 05:30
  • You'll need to have a web server, but luckily there are [plenty of ways to spin up a web server](https://gist.github.com/willurd/5720255) without any configuration. Since you're using PHP, [`php -S`](http://php.net/manual/en/features.commandline.webserver.php) seems like an obvious choice. – Jordan Running Jan 28 '17 at 05:31
  • amazonCut.txt Thanks@GerardoFurtado – SHE Jan 28 '17 at 05:31
  • Your answer doesn't help at all: can you paste the link of the file? – Gerardo Furtado Jan 28 '17 at 05:32
  • 1
    If it is in local drive, then you can take help from this link http://stackoverflow.com/questions/27522979/read-a-local-text-file-using-javascript – Shubhranshu Jan 28 '17 at 05:32
  • It is a local file in my computer@GerardoFurtado – SHE Jan 28 '17 at 05:34
  • What @Shubhranshu said, how ever, if I were you, I would just load the contents of that file into a Javascript variable and go ahead with the experiment, if that's not a possibility then create a
    element and load it with the same data and just hide it.
    – Krishnan Venkiteswaran Jan 28 '17 at 05:46
  • how to write that tho? @Krylor – SHE Jan 28 '17 at 05:54
  • @SHE Storage in d3.html Use it in Jquery as follows $( document ).ready(function() { var amazoncut = $("#id_amazoncut"); // Do what you want with the data }); – Krishnan Venkiteswaran Jan 28 '17 at 10:33

0 Answers0