JavaScript 函数未定义?

标签 javascript

出于某种原因,Firefox 在这段 JS 中抛出“函数未定义”错误:

    $(function() { // on document ready

function updateAlerts() {
   $.ajax({
      url : "/check.php",
      type : "POST",
      data : {
         method : 'checkAlerts'
      },
      success : function(data, textStatus, XMLHttpRequest) {
         var response = $.parseJSON(data);

         // Update the DOM to show the new alerts!
         if (response.friendRequests > 0) {
            // update the number in the DOM and make sure it is visible...
            $('#notifications').show().text(response.friendRequests);
         }
         else {
            // Hide the number, since there are no pending friend requests or messages
            var ablanknum = '0';
            $('#notifications').show().text(ablanknum);
         }

      }
   });
}

function friendRequestAlert() {
   $.ajax({
      url : "/check.php",
      type : "POST",
      data : {
         method : 'sendFriendAlert'
      },
      success : function(data, textStatus, XMLHttpRequest) {
         var response = $.parseJSON(data);

         if (response.theFRAlert !== '0') {
            // Display our fancy Javascript notification.
            $.jgrowl('' + response.theFRAlert + '');
         }

      }
   });
}

function messageAlert() {
   $.ajax({
      url : "/check.php",
      type : "POST",
      data : {
         method : 'sendMessageAlert'
      },
      success : function(data, textStatus, XMLHttpRequest) {
         var response = $.parseJSON(data);

         if (response.theAlert !== '0') {
            // Display our fancy Javascript notification.
            $.jgrowl('' + response.theAlert + '');
            $('#therearemessages').show().text(response.theAlert);
         }

      }
   });
}

});

我检查了我的代码,似乎没有任何错误。

最佳答案

没有理由将您的 3 个函数包装在文档就绪包装器中——这些函数中的任何内容(可能依赖于文档准备就绪)在调用它们之前不会执行。此外,通过将它们包装在 doc ready 中,您迫使它们进入匿名函数的范围内,并且不能从外部使用它们。

不相关,您应该在 $.ajax 调用中将数据类型设置为“json”,并停止对 $.parseJSON 进行手动调用。

新代码:

function updateAlerts()
{
    $.ajax( {
        url: '/check.php',
        type: 'POST',
        data: {
            method: 'checkAlerts'
        },
        dataType: 'json',
        success: function( response )
        {
            // Update the DOM to show the new alerts!
            if( response.friendRequests > 0 )
            {
                // update the number in the DOM and make sure it is visible...
                $( '#notifications' ).show().text( response.friendRequests );
            }
            else
            {
                // Hide the number, since there are no pending friend requests or messages
                var ablanknum = '0';
                $( '#notifications' ).show().text( ablanknum );
            }
        }
    } );
}

function friendRequestAlert()
{
    $.ajax( {
        url: '/check.php',
        type: 'POST',
        data: {
            method: 'sendFriendAlert'
        },
        dataType: 'json',
        success: function( response )
        {
            if( response.theFRAlert !== '0' )
            {
                // Display our fancy Javascript notification.
                $.jgrowl('' + response.theFRAlert + '');
            }
        }
    } );
}

function messageAlert()
{
    $.ajax( {
        url: '/check.php',
        type : 'POST',
        data: {
            method : 'sendMessageAlert'
        },
        dataType: 'json',
        success: function( response )
        {
            if( response.theAlert !== '0' )
            {
                // Display our fancy Javascript notification.
                $.jgrowl('' + response.theAlert + '');
                $('#therearemessages').show().text(response.theAlert);
            }
        }
    } );
}

关于JavaScript 函数未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5300363/

相关文章:

javascript - 如何防止移动设备上的 YouTube IFrame JavaScript API 自动全屏显示?

javascript - 使用 jquery ui datepicker 重新聚焦相同的 datepicker 输入时显示错误的月份和年份

javascript - 使用 JavaScript 获取下拉列表中的选定值

javascript - 如何在 UI 列表中单击按钮时使用颜色获取多个值?

javascript - 如何简化 JavaScript 中的 `(variableA && !variableB) || !variableA` 表达式?

javascript - TypeScript 中的自动 getter 和 setter

javascript - React JS - 传递子组件的函数

javascript - 为图像加载添加类 - 确定是否为横向

javascript - Bootstrap 嵌套可折叠面板

javascript - 有什么方法可以从 JavaScript 访问浏览器表单字段建议吗?