JSON in file: // protocol

So, I have to open my web application, for example a file. Thus, the browser opens the file .html, and in the adres protocol is used file://. But on the script, I need to open the file .jsonand load it into a variable.

I tried a query $.ajaxwith a data type jsonp, I tried requirejswho kind of loads the file, but gets a syntax error from the json file, so it does not load into the variable. The same thing happens if I just do it

<script type="text/javascript" src="path/to/.json"></script>

Is there another way? The application does not use PHP, which must be run from the protocol file://without changing the browser settings.

thanks

+4
source share
2 answers

you can create a file .jsand add data jsonto varand access this var

<script type="text/javascript" src="path/to/data.js"></script>

UPDATE .. EXAMPLE

var text = '{"employees":[' +
'{"firstName":"John","lastName":"Doe" },' +
'{"firstName":"Anna","lastName":"Smith" },' +
'{"firstName":"Peter","lastName":"Jones" }]}';
+4
source

Paste data directly on the page.

There is no network, so it will not lead to a decrease in performance. It is static, so you do not lose the ability to generate it dynamically.

+2
source

Source: https://habr.com/ru/post/1612578/


All Articles