0

i'd like to import this json file in my JavaScript (test.js) without using any server (Node or others).

i searched on google, but i couldn't the correct answer. when i used ajax, it need to allow CORS.

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

data.json file is like this:

[
  {
    "id": 1,
     "name": {
        "first": "Paige",
        "last": "Bools"
            },
      "marks": {
        "math": 80.89,
         "history": 78.23,
         "computer": 68.09
       }
     },       
 ]

in this case, how can i import this json file in my javascript file? please give me any suggestion.

4
  • Can you validate JSON in online json formatter? doesn't look JSON to me Commented Jun 19, 2020 at 20:47
  • What about this: stackoverflow.com/questions/35712158/… Commented Jun 19, 2020 at 20:48
  • If you need to bypass CORS policy, and let's say your JSON's URL is https://example.com/data.json, then you can access it through the CORS-Anywhere service with this URL: https://cors-anywhere.herokuapp.com/https://example.com/data.json Commented Jun 19, 2020 at 20:49
  • What do you mean "without a server"? All files are served up by a web server, either a local or remote. Unless you mean a file on the Client's local drive. Where is this json file stored? Your example looks like a js array with two elements, one object and the other undefined. Commented Jun 19, 2020 at 20:59

1 Answer 1

0

The format is an array of objects. You can store it in an array in the same javascript file or if the data is big save it in another js file by just defining a variable and loading it using script tag.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.