English is not my first language, and I struggle with it, but I did my best.
I am attempting to include a js file that imports json from an html
.js
import menus from '../json/menus.json';
(function () {
function parseMenu(ul, menu) {
for (var i=0;i<menu.length;i++) {
if(menu.sub == null) {
menu.sort(function(a, b) {
return a.id > b.id ? -1 : a.id > b.id ? 1 : 0;
});
}
var li=$(ul).append('<li><a href="'+menu[i].link+'">'+menu[i].name+'</a></li>');
if (menu[i].sub!=null) {
var subul=$('<ul id="submenu'+menu[i].link+'"></ul>');
$(li).append(subul);
parseMenu($(subul), menu[i].sub);
}
}
}
var menu=$('#menu');
parseMenu(menu, menus);
});
.json form
[ {
{
},
{
}
},
{
{
},
{
}
},
{
}
]
I attempted the following
<script type="module" src="../js/left.js"></script>
<script type="module" src="../json/menu.json"></script>
in HTML
However, I encountered the error message: Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "application/json". Strict MIME type checking is enforced for module scripts per HTML spec. in .json
I managed to solve it.
First. Changed name of variable from JSON to menus
Second. Deleted
<script type="module" src="../js/left.mjs"></script>
Last. Changed
import JSON from '../json/menu.json';
to import menus from '../json/menus.json' assert { type: "json" };