javascript $(...).getJSON不是函数

lndjwyie  于 2023-05-05  发布在  Java
关注(0)|答案(4)|浏览(111)

我试图开发一个简单的API调用,它以JSON响应的形式返回我的注解,但是当我点击它时,我得到了错误
$(...).getJSON is not a function
我的想法是,当我点击按钮“注解”(id=showarea)时,它会立即打印答案和文本区域的注解。
我在文件上“硬编码”只是为了测试。
我有这个文件(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);
            });
        });
    });
}

但是当没有任何事情发生时,它甚至不识别点击,然后我改变了硬编码的方式,错误发生了。我检查了我的jquery include,一切似乎都包含在内。
这是我试图加载注解的文件。你能发现什么不对劲吗?
我很抱歉,如果这是极端的新手,但我一直强调这种方式太多。
亲切的问候

<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')}
xv8emn3q

xv8emn3q1#

这可能不是这里的特殊问题,但是如果使用jQuery的“精简”版本,其中不包括 AJAX ,动画等,则会出现同样令人困惑的错误。如果“.slim”出现在包含jQuery的行中,请尝试将其删除。
另外,检查浏览器控制台以确保jQuery正确加载并且没有得到404或类似的错误。

q3aa0525

q3aa05252#

也许改变

$("#showarea").getJSON

$.getJSON
lymnna71

lymnna713#

您可以尝试:

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

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

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

von4xj4u4#

可能是由于jQuery版本的原因,需要更改

$.getJSON

jQuery.getJSON

另一种选择是丢弃**$.getJSON并使用$. AJAX 作为(根据jQuery文档)$.getJSON**是一个简写的Ajax函数,它相当于:

$.ajax({
    dataType: "json",
    url: url,
    data: data,
    success: success
});

相关问题