Giter Club home page Giter Club logo

json-file-importer's Introduction

json-file-importer

Description

This is a simple example of how to import a JSON file from a HTML file with simple JavaScript code.

Typical errors for this use case

Here are some problems you can encounter when trying to find a solution to import JSON files into a HTML file.

Access to fetch at 'file:////json-file-importer/src/example.json' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.

The error you're seeing is due to a security feature in web browsers called the Same-Origin Policy. This policy prevents web pages from making requests to resources on a different domain, port, or protocol than the one that served the page.

Here, you're trying to fetch a file from the local file system using the file:// protocol, which is not allowed by the Same-Origin Policy. To fix this, you can serve your HTML and JSON files from a local web server instead of opening them directly in the browser.

One way to do this is to use the http-server package from npm. Here's how you can install and use it:

  1. Install http-server globally using npm:

    npm install -g http-server
    

    or using Homebrew (MacOS):

    brew install http-server
    
  2. Navigate to the directory containing your HTML and JSON files in the terminal.

  3. Start the server by running the following command:

    http-server
    
  4. Open your browser and navigate to http://localhost:8080/index.html (replace index.html with the name of your HTML file).

This should serve your HTML and JSON files from a local web server, which should allow you to fetch the JSON file without encountering the CORS error.

If you want to specify a different port, you can use the -p or --port option followed by the desired port number. For example, to start the server on port 3000, you can run the following command:

http-server -p 3000

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.