【问题标题】:Function Statements no name error函数语句没有名称错误
【发布时间】:2014-12-07 03:41:34
【问题描述】:

我有以下代码:

<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>

但是,我的控制台一直给我错误:“SyntaxError: Function statements must have a name。” 我似乎无法解决这个问题,这就是 AJAX 代码没有运行的原因。这个错误来自哪里?

按照 Todd 所说,我将代码更改为:

  <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: undefined is not a function (evalating 'self.html("Loading please wait...")')

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

【问题讨论】:

  • 您在实际代码中是否缺少(document).on() 之前的JQuery 别名(即$)?
  • 啊哈,我加进去了,还是一样的错误
  • 问题,self 是在某处定义的...我设置为var self = this; ...看不到它。问题可能是 self.html ...
  • self 声明在哪里?
  • 嗨,我试过 var self=this,但它仍然返回相同的错误

标签: javascript jquery ajax


【解决方案1】:

根据我的评论

$(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
    });
});

【讨论】:

    【解决方案2】:

    您无需使用变量即可解决问题。只需设置$.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...");
        }
    });
    

    【讨论】:

      【解决方案3】:

      您发布的代码是正确的。错误必须来自其他地方。也就是说,无论错误在哪里,都需要查找以下内容:

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

      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 对象。

      【讨论】:

        猜你喜欢
        • 2021-10-21
        • 1970-01-01
        • 1970-01-01
        • 2021-01-11
        • 1970-01-01
        • 2022-11-10
        • 2014-06-22
        • 1970-01-01
        • 2011-12-26
        相关资源
        最近更新 更多