Uncaught ReferenceError: Papa is not defined. Loaded in from cdn in header

Uncaught ReferenceError: Papa is not defined. Loaded in from cdn in header

下面是HTML。我在 header 中加载了 PapaParse 的 cdn 托管库。但是当我 运行 我的 JavaScript 文件和它 运行 我调用 Papa.unparse(data); 的函数时,它会抛出一个错误,指出没有定义 Papa。所以 我对这里的确切问题是什么感到有点困惑,因为我显然已经将它加载到 html 并且在我的 JavaScript 脚本加载之前。 非常感谢帮助。

编辑: 我已经解决了这个问题。问题出在 cdn 本身。我假设链接已损坏或文件不在他们的服务器上。也可能是那个特定版本,我怀疑这很可能是原因。使用 cdnjs 测试相同版本仍然不起作用。奇怪。

<script src="https://fastcdn.org/Papa-Parse/4.1.2/papaparse.min.js"></script>

这个版本和cdn好像可以用。谢谢!

   

 function makeThatExcelFilePapa(data, title) {
        var csv = Papa.unparse(data);
        return csv;
    }
// Make sure the DOM is loaded before trying to add listeners
window.onload = function () {
    // Set dropdown list for Brand to be empty on start
    document.getElementById("dealerSearchForm").elements["brand"].selectedIndex = -1;

    // Call submit method when submit button is clicked
    var submitButton = this.document.getElementById("submitButton");
    $(submitButton).on('click', getFormData);
    $(exportToExcel).on('click', callJSONConverter);
}

var items;
function callJSONConverter() {
    //JSONToCSVConvertor(JSON.stringify(items), "Results for " + dealerName, true);
    makeThatExcelFilePapa(items, "Results for " + dealerName);
    return false;
}
   

 <!DOCTYPE html>
    <html>

    <head>
        <meta charset="UTF-8">
        <title>Dealer Tab</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.css" />
    <link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid-theme.min.css" />
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.js"></script>
    <script type="test/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.6.0/papaparse.min.js"></script>
    <link rel="stylesheet" href='cnh_dealer_tab.css'>
        
        
    </head>

    <body>
        <div class="form-style">
            <h3>Find Dealer:</h3>
            <form id="dealerSearchForm" method="POST" action="cnh_dealer_tab.html">
                    
                    Brand:
                    <select name="Brand" id="brand" required>
                        <option value="NHAG">NHAG</option>
                        <option value="NHCE">NHCE</option>
                        <option value="CSCE">CSCE</option>
                        <option value="CSAG">CSAG</option>
                    </select>
                    <div id="goRight">
                        SAP Code:
                    <input type="text" id="sapCode" name="SAPCode" required>
                </div>
                <br>
                <br> Dealer:
                <input type="text" id="dealer" value="" readonly>
                <br>
                <br>
                <button id="submitButton">Submit</button>
                <input type="reset" name="reset">
                <button id="goToCountyTab">Go to County Tab</button>
                <button id="exportToExcel">Export to Excel</button>

            </form>
        </div>
        <div id="jsGrid" class="form-style">
            <span class="grid">
                <script type="text/javascript" src='cnh_grid_data'></script>
                <script type="text/javascript" src='cnh_ssm_assignments'></script>
            </span>
        </div>
    </body>

    </html>
谢谢

尝试做:

$(document).ready(function() {
    function makeThatExcelFilePapa(data, title) {
        var csv = Papa.unparse(data);
        return csv;
    }
});

并且,正如其他人所指出的,不要加载 Papa 库两次。