Search code examples
javascriptajaxjsonwebserver

Do we need a web server (like Apache) to access a .json file?


I was trying to read an info.json file, using the jQuery API. Please find the code below, which is part of test.html.

$.getJSON('info.json', function(data) {
  var items = [];
  $.each(data, function(key, val) {
    items.push('<li id="' + key + '">' + val + '</li>');
  });

The test.html file resides on my local machine and when I try to open it in the browser, the Ajax call is not getting triggered and the info.json file is not read.

Is it not working because I don't have a web server? Or am I doing anything wrong in the code? (I don't see any errors in the Firebug console though).

Thanks in advance.


Solution

  • You will always have to host your site from where you are making AJAX call. Otherwise it will throw this exception.

    origin null is not allowed by access-control-allow-origin
    

    Host your page on localhost server and I guess everything will work nicely.