0

i have html file with javascript, app for driving tests and 2 json files: Categories of name of test and questions with answer it self. this json is dump of sqlite database. example output below.

[
    {
        "cat_id": "1",
        "cat_name": "Общие понятия",
        "cat_score": "2"
    },
    {
        "cat_id": "2",
        "cat_name": "Обязанности водителя",
        "cat_score": "7"
    },
    {
        "cat_id": "3",
        "cat_name": "Cостояние здоровья водителя",
        "cat_score": "1"
    }
]

in my engine.js everything works but my questions and categories files are not loaded. i don't know how to load then and parse to array or variable to use it in script.

for example in php i have

require and include or file_get_content but how i need to load

So i found how to load but it not working

function load() {

    var someData_notJSON = JSON.parse(data);
    console.log(someData_notJSON); 
}

my html file is

<script type="text/javascript" src="./Categories.json"></script>
<script type="text/javascript" src="./Questions.json"></script>
  • didn't get any part of your question – Muhammad Usman Jan 20 '19 at 16:11
  • 1
    Please be clear about what you want to achieve and the problem you are currently facing. What you posted *is* valid JavaScript, so you can include that in your page. If you mean you want to load it from a file, you can Google AJAX. – Mitya Jan 20 '19 at 16:12
  • i'm update, it's simple javascript. i'm noob in this – Mixmax Makster Jan 20 '19 at 16:19
  • *"it not working"*: please be specific. What is the value of `data`, what is the problem? Do you get the wrong result? An error? ... – trincot Jan 20 '19 at 16:19
  • Uncaught ReferenceError: data is not defined at load (engine.js:7) at onload (index.html:12) load @ engine.js:7 onload @ index.html:12 – Mixmax Makster Jan 20 '19 at 16:22

1 Answers1

0

in your case you're trying to parse JSON from undefined data.

function load(data) {
    var someData_notJSON = JSON.parse(data);
    console.log(someData_notJSON); 
}

load(`[
    {
        "cat_id": "1",
        "cat_name": "Общие понятия",
        "cat_score": "2"
    },
    {
        "cat_id": "2",
        "cat_name": "Обязанности водителя",
        "cat_score": "7"
    },
    {
        "cat_id": "3",
        "cat_name": "Cостояние здоровья водителя",
        "cat_score": "1"
    }
]`);
Sebastian Waldbauer
  • 674
  • 1
  • 10
  • 17