函数语句无名称错误

Function Statements no name error

本文关键字:错误 无名 语句 函数      更新时间:2023-09-26

我有以下代码:

<script type="text/javascript">
    $(document).on("click", "#leftconversation", function(){
        var self = this;
        var cid = $(this).attr('class'); // getting the user id here
        var request = $.ajax({
            url: "conversation.php",
            type: "POST",
            data: { cid: cid },
            beforeSend: function(){
                self.html("Loading please wait...");
            }
        });
        //WHEN SUCCESS
        request.success(function( data ) {
            $("#right").html(data); // replace the right div with echoed content from php file
        });
    });
</script>

但是,我的控制台不断给我错误:"语法错误:函数语句必须有一个名称。我似乎无法解决问题,这就是 AJAX 代码无法运行的原因。此错误从何而来?

根据托德所说,我将代码更改为以下内容:

  <script type="text/javascript">
            $(document).on("click", "#leftconversation", function(){
                var self = this;
    var cid = $(this).attr('class'); //you are getting the user id here
     var request = $.ajax({
                        url: "conversation.php",
                        type: "POST",
                        data: { cid: cid },
                        beforeSend: function(){
                            self.html("Loading please wait...");
                        },
                   success: function(data) { 
                   $("#right").html(data);
                   },
                   error: function(request, err){ console.log('An Error Occured' + err); }
                 });



});
</script>

修复了第一个错误,但现在它告诉我 TypeError:未定义不是一个函数(评估"self.html("正在加载请稍候..."('(

这是固定的,应该使用 var self = $(this(;

根据我的评论

$(document).on("click", "#leftconversation", function(){
    var $self = $(this);
    var cid = $(this).attr('class'); // getting the user id here
    var request = $.ajax({
        url: "conversation.php",
        type: "POST",
        data: { cid: cid },
        beforeSend: function(){
            $self.html("Loading please wait...");
        }
    });
    //WHEN SUCCESS
    request.success(function( data ) {
        $("#right").html(data); // replace the right div with echoed content from php file
    });
});

您可以解决问题,而无需使用变量。只需设置$.ajax调用的 context: 属性即可。

var request = $.ajax({
    url: "conversation.php",
    type: "POST",
    data: { cid: this.className }, // Quicker way to get the class.
    context: $(this), // The context in the callback will be the jQuery object.
    beforeSend: function() {
    //   v-- This is now a jQuery object.
        this.html("Loading please wait...");
    }
});

你的代码,正如你发布的那样,是正确的。错误必须来自其他地方。也就是说,无论错误在哪里,这里都是要查找的内容:

您可能知道,函数可以像这样定义:

function greet(name) { /* ... */ }

这在语句上下文中工作。函数也可以在表达式上下文中使用:

[1, 2, 3].forEach(function(item) { alert(item); });

在表达式上下文中,我们可以省略名称,就像我们上面所做的那样,或者我们可以包含一个名称:

[1, 2, 3].forEach(function foo(item) { alert(item); });

但是,我们不能做的是拥有一个没有名称的独立函数声明。这是一个错误:

function(name) { /* ... */ }

这就是你(现在第一个(的问题。


" undefined不是函数">

更新后的代码存在其他问题。当你设置self = this时,this是一个 DOM 元素,而不是一个 jQuery 对象。您稍后尝试使用 self.html ,但 DOM 元素没有 html 属性。如果你想使用jQuery方法,你必须在赋值点(self = $(this)(或使用点$(self).html将元素转换为jQuery对象。