Function.prototype.apply:参数列表的类型错误

Function.prototype.apply: Arguments list has wrong type

本文关键字:类型 错误 列表 参数 prototype apply Function      更新时间:2023-09-26

The error in the title of the post came from jQuery version 1.10.2, line 637我有一个模态,在单击带有一些文本框的按钮时弹出,当单击模态内的按钮时,文本框中的信息将通过 AJAX 添加到数据库中。 为了使页面更加用户友好,我添加了一个 setTimeout 函数来暂停模态的隐藏,以便用户可以看到数据已添加到数据库的验证消息。 我的代码块 1 将记录添加到数据库中,但 setTimeout 调用无法正常工作:

 function insert(data) {
                data = JSON.stringify(data);
                $.ajax({
                    type: "POST",
                    url: "../Service.asmx/InsertPerson",
                    dataType: "json",
                    contentType: "application/json",
                    data: data,
                    //record gets added to the database
                    //something about the setTimeout function 
                    //that gives the error in the title
                    success: function () {
                        console.log('success before setTimeout');
                        var successMessage = $('<div>').text('Successfully added to the database...').css('color', 'green');
                        $('.modal-body').append(successMessage);
                        //*******this function doesn't run
                        window.setTimeout(function () {
                            $('#contact').modal('hide');
                            $('.modal-body input').each(function () {
                                $(this).val('');
                            }, 1000);
                        });
                    }
                });
            }

我使用以下代码修复了它:(成功函数是我们这里需要注意的)

function insert(data) {
                data = JSON.stringify(data);
                $.ajax({
                    type: "POST",
                    url: "../Service.asmx/InsertPerson",
                    dataType: "json",
                    contentType: "application/json",
                    data: data,
                    //record gets added to the database
                    success: function () {
                        console.log('success before setTimeout');
                        var successMessage = $('<div>').text('Successfully added to the database...').css('color', 'green');
                        $('.modal-body').append(successMessage);
                        window.setTimeout(function () {
                            $('.modal-body input').each(function () {
                                $(this).val('');
                            });
                            $('#contact').modal('hide');
                        }, 1000);
                    }
                });
            }

我看到我在第一个块中没有关闭each函数,我在第二个块中修复了它,这就是它工作的原因,但为了将来参考,这个错误在这种情况下到底意味着什么?

这意味着您将第二个参数留给了setTimeout,而是将其作为第二个参数传递给.each()

编辑 — 看起来 jQuery 正在拾取参数(1000)并试图将其传递给其内部each实现。.apply()函数期望它是一个数组。