javascript jQuery Uncaught TypeError in $(selector).on()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31876769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:30:19  来源:igfitidea点击:

jQuery Uncaught TypeError in $(selector).on()

javascriptjqueryjavascript-events

提问by David Gomes

I am getting the following error:

我收到以下错误:

Uncaught TypeError: ((jQuery.event.special[handleObj.origType] || (intermediate value)).handle || handleObj.handler).apply is not a function

This is my code and it's somewhere in between the beforeSendand the successcalls that the error is happening:

这是我的代码,它位于发生错误beforeSendsuccess调用和调用之间:

$('#main-container').on('click', '#sign-in-btn', function (event) {
    var username = $('#username-textbox').val();
    var password = $('#password-textbox').val();

    $.ajax({
        url: '/login',
        method: 'POST',

        beforeSend: function (xhr) {
            xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password));
        },

        success: function (data) {
        }
    });

    event.preventDefault();
});

I also tried to put inside a $(document).ready(function () { ... }call and I get the exact same error.

我还尝试$(document).ready(function () { ... }拨打电话,但得到了完全相同的错误。

回答by David Gomes

Having the event function return falsefixed it because the error was somewhere during the propagation of the event and if an event returns falsethen event.stopPropagation()is "called".

具有事件函数返回false固定的,因为错误的事件的传播过程中,如果一个事件的回报是什么地方false,然后event.stopPropagation()被“叫”。