$(...).getJSON不是一个函数

Luí*_*sta 8 javascript jquery

我正在尝试开发一个简单的API调用,它在JSON响应中返回我的注释,但是当我点击它时,我得到了错误

$(...).getJSON is not a function

我的想法是,当我点击"评论"按钮(id = showarea)时,它立即打印出该答案和textarea的评论.

我在文件上"硬编码"只是为了测试.

我有这个文件(javascript/askme/comment.js)

function initCommentReloader() {
    $('#textarea').on('click', 'a', function() {
        $.getJSON("/controller/api/comments/comment.php", {answerid: answerid}, function(data) {
            $.each(data, function(i, comment) {
                console.log(comment);
            });
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

但当没有任何事情发生时,它甚至不识别点击,然后我改为硬编码方式,错误发生了.我检查了我的jquery包含,一切似乎都包含在内.

这是我正在尝试加载评论的文件.你能找到什么问题吗?

我很抱歉,如果这是一个极端的新手,但我一直在强调这种方式.

亲切的问候

 <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">

    <title>AskMe</title>

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">


    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <script>
        $( function() {
            $( "#tabs" ).tabs();
        } );
    </script>

    <!-- CSS -->
    <link rel="stylesheet" type="text/css" href="{$BASE_URL}css/fonts/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="{$BASE_URL}css/fonts/font-awesome.css">
    <link rel="stylesheet" type="text/css" href="{$BASE_URL}css/styles/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="{$BASE_URL}css/styles/main.css">
    <link rel="stylesheet" type="text/css" href="{$BASE_URL}css/styles/responsive.css">
    <link rel="stylesheet" type="text/css" href="{$BASE_URL}css/styles/clean-blog.min.css">

    <!-- JS -->
    <script type="text/javascript" src="{$BASE_URL}javascript/vendor/bootstrap.js"></script>
    <script type="text/javascript" src= "{$BASE_URL}javascript/Chart.js"></script>
    <script type="text/javascript" src="{$BASE_URL}javascript/main.js"></script>
    <script src="https://cdn.ckeditor.com/4.6.2/standard/ckeditor.js"></script>

</head>


<div class="post-button clearfix">
                <button class="btn icon-chat" title="Add a comment on this answer"
                        type="submit" id="showarea" name="showarea" value="Show Textarea">
                    Comment</button>
                <div id="textarea">
                    {include file="comment.tpl"}
                    {include file="comment_form.tpl"}
                </div>
            </div>

<script>
    answerid = {$answer['answerid']};
    console.log();
    $("#showarea").click(function() {
        $("#showarea").getJSON("/controller/api/comments/comment.php", function (data) {
            console.log(data);
        });
    });
</script>
{HTML::script('askme/comment.js')}
Run Code Online (Sandbox Code Playgroud)

WBT*_*WBT 9

这可能不是这里的特定问题,但如果使用排除 ajax、动画等的“slim”版本的 jQuery,也会出现同样的令人困惑的错误。如果“.slim”出现在包含 jQuery 的行中,请尝试将其删除.

此外,请检查浏览器控制台以确保 jQuery 加载正确且未出现 404 或类似错误。


Ash*_*man 1

你可以试试:

$(document).on('click', '#textarea>a', function() {
  $.getJSON(
    "/controller/api/comments/comment.php", 
    {answerid: answerid},
    function(data) {
      $.each(data, function(i, comment) {
        console.log(comment);
      });
    }
  );
});
Run Code Online (Sandbox Code Playgroud)

或者

$(document).on('click', '#textarea>a', function() {
  $.getJSON( "/controller/api/comments/comment.php", {
    answerid: answerid
  }).done(function( data ) {
    $.each(data, function(i, comment) {
      console.log(comment);
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

或者

$(document).on('click', '#textarea>a', function() {
  $.ajax({
    dataType: "json",
    url: "/controller/api/comments/comment.php",
    data: {answerid: answerid},
    success: success
  }).done(function( data ) {
    $.each(data, function(i, comment) {
      console.log(comment);
    });
  });
});
Run Code Online (Sandbox Code Playgroud)