开发者问题收集

jquery.js 文件版本 3.6 在单击按钮时抛出错误

2023-12-05
73

我有一个在客户端使用 jQuery 的应用程序。它一直运行良好,但我开始在 jquery 文件中收到错误,尽管代码没有更改。

这是按钮代码:

<a id="lookup" href="#SummaryModal" type="button" data-toggle="modal" data-backdrop="static" class="btn btn-tertiary" title="Summary"><span class="glyphicon glyphicon-list" aria-hidden="true"></span>Quick Summary</a>

这是脚本代码,但在控件到达此代码之前,以下错误就会被抛出。

$(document).on("click", "#lookup", function (e) {
        $.ajax({
            url: "/Home/GetSummary/",
            type: 'POST',                
            beforeSend: function (xhr) {
                xhr.setRequestHeader("XSRF-TOKEN",
                    $('input:hidden[name="__RequestVerificationToken"]').val());
            },
            cache: true,
            async: true

        }).done(function (result) {
            $("#divSummary").html(result);
        });
    })

单击它时,我收到以下错误:

Uncaught TypeError: Cannot read properties of undefined (reading 'jQuery360059256788808060092')

at Data.get (jquery.js:4301:9)

at Data.access (jquery.js:4319:16)

at Function.data (jquery.js:4459:19)

at HTMLAnchorElement.delegate (jquery.validate.js:419:23)

at HTMLFormElement.dispatch (jquery.js:5430:27)

at elemData.handle (jquery.js:5234:28)

at Object.trigger (jquery.js:8719:12)

at Object.simulate (jquery.js:8788:16)

at HTMLDocument.handler (jquery.js:8822:17)

get @ jquery.js:4301

access @ jquery.js:4319

data @ jquery.js:4459

delegate @ jquery.validate.js:419

dispatch @ jquery.js:5430

elemData.handle @ jquery.js:5234

trigger @ jquery.js:8719

simulate @ jquery.js:8788

handler @ jquery.js:8822

我已附上相同的屏幕截图。我尝试更新 jquery 文件以查看它是否与 jquery 版本有关,但它仍然会抛出相同的错误。我已经检查了 jqueryvalidate1.17 与 jquery3.6 的兼容性,似乎没有问题。

有什么关于如何修复它的线索吗?

图片链接: https://imgur.com/a/LcRdwD5

2个回答

我建议您通过 consol.log 检查这两个变量: $('input:hidden[name="__RequestVerificationToken"]').val()result 。如果其中一个变量未定义,您就会知道出现此错误的原因。以下是此错误代码的解释:

Causes for TypeError: Cannot Read Property of Undefined The error clearly says that it is Undefined, which means the variable might be declared or used. Still, there is no value associated with the variable. In short, the value is not assigned.

In JavaScript, properties or functions are Objects, but undefined is not an object type. If you call the function or property on such variable, you get the error in console TypeError: Cannot read undefined properties.

Dump
2023-12-06

我知道这是一个奇怪的修复,我无法解释为什么它会起作用,但我有其他几个按钮类型的链接,它们工作正常。因此,这个链接和其他链接之间的唯一区别是 type = 'button' 代码的存在。

因此,以下代码:

<a id="lookup" href="#SummaryModal" type="button" data-toggle="modal" data-backdrop="static" class="btn btn-tertiary" title="Summary"><span class="glyphicon glyphicon-list" aria-hidden="true"></span>Quick Summary</a>

变成

<a id="lookup" href="#SummaryModal" data-toggle="modal" data-backdrop="static" class="btn btn-tertiary" title="Summary"><span class="glyphicon glyphicon-list" aria-hidden="true"></span>Quick Summary</a>

瞧,它起作用了。与 jquery 版本或 jquery 文件无关。

TheFallenOne
2023-12-11