如何将JSON文件放在本地服务器上?

时间:2021-08-04 17:48:59

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:

在过去的两个小时里,我一直在疯狂地试图找到我遇到的问题的答案。我试图访问一个名为data.json的本地JSON文件,它放在我的项目目录中。我的控制台返回了此错误:

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

交叉源请求仅支持协议方案:http,数据,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.

我现在知道我无法在本地访问此文件,我必须通过外部服务器访问此文件。如何使用此文件设置本地服务器?有人可以解释如何使用python,json-server和node,js吗?我完全迷失了。

Here is my code:

这是我的代码:

$(function (){

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

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

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

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

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

1 个解决方案

#1


2  

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.

可以使用单个命令启动Python中非常简单的Web服务器。这将允许您通过HTTP获取内容。

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

从data.json文件所在的目录,您可以运行以下某个...

  • if you are running Python 2:

    如果您正在运行Python 2:

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

    或者如果您正在运行Python 3:

    • $ python -m http.server 8000
    • $ python -m http.server 8000

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

启动服务器后,它将侦听端口8000上的请求。

You would access it by sending an HTTP GET request to localhost (http://127.0.0.1:8000/data.json)

您可以通过向localhost发送HTTP GET请求来访问它(http://127.0.0.1:8000/data.json)

#1


2  

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.

可以使用单个命令启动Python中非常简单的Web服务器。这将允许您通过HTTP获取内容。

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

从data.json文件所在的目录,您可以运行以下某个...

  • if you are running Python 2:

    如果您正在运行Python 2:

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

    或者如果您正在运行Python 3:

    • $ python -m http.server 8000
    • $ python -m http.server 8000

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

启动服务器后,它将侦听端口8000上的请求。

You would access it by sending an HTTP GET request to localhost (http://127.0.0.1:8000/data.json)

您可以通过向localhost发送HTTP GET请求来访问它(http://127.0.0.1:8000/data.json)