JavaScript / HTML:如何导入本地JSON /理解模块访问

发布于 2025-02-02 02:48:30 字数 1103 浏览 3 评论 0原文

我是JavaScript / HTML的新手,并且有一些问题可以理解导入 /模块功能。我需要将JSON文件加载到JavaScript中。

我能找到的最好的方法是通过导入,例如:

  import myJason from './dummy.json' assert {type: 'json'};

尝试时,我会遇到以下错误:

  Cannot use import statement outside a module

因此我将其放入模块(二手标头和车身)中。 但是我无法访问模块功能。

Google无法帮助我,可以请有人解释如何正确执行此操作。

注意:HTML和JSON文件是本地文件(无法通过Web服务器访问)

这是(减少)代码:

    <html lang="en">
        <head>
            <script type="module">
                import myJason from './dummy.json' assert {type: 'json'};
                function dummy(){
                    //evaluate JSON
                    return result();
                }
                window.dummy = dummy;
                export{dummy}

            </script>
        </head>
        <body>


            <script>
                // tried (and other things)
                //   dummy();
                //  window.dummy();
            </script>
        </body>
    </html>





  

I am new to javascript / html and have some problems to understand the import / module functionality. I need to load a json file into javascript.

The best way I could find is via import, like:

  import myJason from './dummy.json' assert {type: 'json'};

When trying it, I am getting following error:

  Cannot use import statement outside a module

So I placed it into a module (tried header and body).
But I am not able to access the module functions.

Google could not help me, could please somebody explain how to do it properly.

Note: HTML and JSON files are local files (not accessed via Webserver)

Here is the (reduced) code:

    <html lang="en">
        <head>
            <script type="module">
                import myJason from './dummy.json' assert {type: 'json'};
                function dummy(){
                    //evaluate JSON
                    return result();
                }
                window.dummy = dummy;
                export{dummy}

            </script>
        </head>
        <body>


            <script>
                // tried (and other things)
                //   dummy();
                //  window.dummy();
            </script>
        </body>
    </html>





  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戒ㄋ 2025-02-09 02:48:30

尝试使用XML httprequest

var xhttp = new XMLHttpRequest();

function dummy(){
    if (this.readyState == 4 && this.status == 200) {
       return JSON.parse(xhttp.responseText)
    }
}

xhttp.onreadystatechange = dummy
xhttp.open("GET", "/dummy.json", true);
xhttp.send();

,甚至更好地fetch api检查在这里

Try using XML HttpRequest

var xhttp = new XMLHttpRequest();

function dummy(){
    if (this.readyState == 4 && this.status == 200) {
       return JSON.parse(xhttp.responseText)
    }
}

xhttp.onreadystatechange = dummy
xhttp.open("GET", "/dummy.json", true);
xhttp.send();

or even better the fetch api check that here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文