JS和JSON文件/mimetype扩展名的Python Flask ES6导入

2024-04-29 07:17:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想对JSON文件使用ES6导出/导入

    // ui.js
    import sampleData from '../data/sample-data.json';
    
    export function initUI() {
      console.log(sampleData)
    }

我有一个烧瓶应用程序,在main.py

我添加了以下内容(我还尝试切换顺序)


    mimetypes.add_type('application/json', ".json", strict=True)
    mimetypes.add_type('application/javascript', ".js", strict=True)

这只适用于JS文件。对于JSON导入,我得到以下错误

Failed to load module script: The server responded with a non-JavaScript MIME type of "application/json". Strict MIME type checking is enforced for module scripts per HTML spec.

我的想法是因为json有前缀js。因此,所有json导入都被视为.js文件

我怎样才能解决这个问题


Tags: 文件addjsontruedataapplicationtypejs