pghInitechBranch pghInitechBranch - 1 year ago 122
Node.js Question

How to put JSON file on a local server?

I've driven myself insane for the last two hours attempting to find an answer for the issue that I'm encountering. I was trying to access a local JSON file called, data.json, which I placed inside my project directory. My console returned this error:

Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

I now know that I cannot access this file locally and I must do so through an external server. How does someone setup a local server with this file? Can someone please explain how to do it using python, json-server, and node,js? I am completely lost.

Here is my code:

$(function (){

var $orders = $("#orders");
var $name = $('#name');
var $drink = $('#drink');

type: 'GET',
datatype: 'json',
url: 'data.json',
success: function(orders) {
$.each(orders, function(i, order) {
$orders.append('<li>Name: ' + + ', Drink: ' + order.drink + '</li>');
error: function() {
alert('error loading orders');

$('#add-order').on('click', function() {

var order = {
name: $name.val(),
drink: $drink.val()

type: 'POST',
url: 'data.json',
data: order,
success: function(newOrder) {
$orders.append('<li>Name: ' + + ', Drink: ' + newOrder.drink + '</li>');
error: function () {
alert("error saving order");

Answer Source

How does someone setup a local server with this file?

A very simple web server in Python can be launched with a single command. This would allow you to fetch the contents over HTTP.

From the directory where your data.json file resides, you can run one of the following...

  • if you are running Python 2:

    • $ python -m SimpleHTTPServer 8000
  • or if you are running Python 3:

    • $ python -m http.server 8000

After launching the server, it will be listening for requests on port 8000.

You would access it by sending an HTTP GET request to localhost (

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download