【问题标题】:You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. C#您可能需要适当的加载程序来处理此文件类型,目前没有配置加载程序来处理此文件。 C#
【发布时间】:2020-04-30 14:55:42
【问题描述】:

我正在尝试在 javascript 中使用 $.getJSON() 函数,但出现此错误:

"你可能需要一个合适的加载器来处理这个文件类型, 目前没有配置加载器来处理这个文件。”

这是我的javascript:

$(document).ready(function () {
    console.log("Hello from riskanalysis.delete.js");

    var categoryTd = $('#categoryId');
    var categoryText = categoryTd.text();
    var categoryInt = parseInt(categoryText);
    console.log(categoryInt);
    console.log(categoryText);

        // Send an AJAX request
    $.getJSON("/riskanalysis/getCategoryNameById?id=" + categoryInt)
    console.log("Hello before");
            .done(function (categoryName) {
                // On success
                console.log("Category name is: " + categoryName);
                categoryTd.text(categoryName); 
            });

    console.log("Hello after");
});

这是我的webpack.config.js 文件:

module.exports = {
    entry:
    {
        shared: './src/shared.js',
        home: './src/home/home.js',
        riskanalysisSearch: './src/riskanalysis/riskanalysis.search.js',
        riskanalysisCreate: './src/riskanalysis/riskanalysis.create.js',
        riskanalysisDelete: './src/riskanalysis/riskanalysis.delete.js',
        dropdown: './src/dropdown/dropdown.js',
        actionplan: './src/actionplan/actionplan.js'
    },
    output: {
        filename: '../wwwroot/js/[name].js'
    },
    optimization: {
        splitChunks: {
            cacheGroups: {
                vendor: {
                    test: /[\\/]node_modules[\\/](jquery)[\\/]/,
                    name: 'vendor',
                    chunks: 'all'
                }
            }
        }
    }

};

这是错误:

奇怪的是,在我所有的其他 javascript 文件中,它都运行良好。

有谁知道可能出了什么问题?已经谢谢了!

【问题讨论】:

    标签: javascript c# asp.net-mvc webpack webpack.config.js


    【解决方案1】:

    错误的原因是console.log("Hello before");这行,把这行移到$.getJSON("/riskanalysis/getCategoryNameById?id=" + categoryInt)上面,使它看起来像这样:

    console.log("Hello before");
    $.getJSON("/riskanalysis/getCategoryNameById?id=" + categoryInt)
            .done(function (categoryName) {
                // On success
                console.log("Category name is: " + categoryName);
                categoryTd.text(categoryName); 
            });
    
    console.log("Hello after");
    

    显示此错误的行是:Module parse failed: Unexpected token (13:12)

    关于错误You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.,这是因为这个文件的完整文件扩展名是.delete.js,这使得webpack认为这不是一个正常的.js文件。 Webpack 将尝试为.delete.js 文件查找文件加载器,因为不存在它会退回到.js 文件加载器。由于上述错误,js解析器将失败导致此错误。

    【讨论】:

    • 非常感谢,这已经解决了这个问题。 .edit.js 和 .create.js 文件不会发生这种情况很奇怪?
    • Webpack 将尝试将这些解析为普通的 .js 文件并成功,因为它们不包含任何错误。如果你故意在这些文件中放入错误并运行 webpack,那么你会得到同样的 webpack 错误。
    猜你喜欢
    • 2021-08-11
    • 2020-01-15
    • 2021-11-21
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2021-04-19
    • 2021-12-11
    • 2020-06-22
    相关资源
    最近更新 更多