简体   繁体   English

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

[英]$(…).getJSON is not a function

I'm trying to develop a simple API call that returns my comments in a JSON response but when I click on it I get the error 我正在尝试开发一个简单的API调用,它在JSON响应中返回我的注释,但是当我点击它时,我得到了错误

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

My idea is, when I click on the button "Comment"(id=showarea) it immediately prints the comments from that answer and the textarea. 我的想法是,当我点击“评论”按钮(id = showarea)时,它立即打印出该答案和textarea的评论。

I'm "hardcoding" on the file just for testing. 我在文件上“硬编码”只是为了测试。

I had this file (javascript/askme/comment.js) 我有这个文件(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);
            });
        });
    });
}

but when nothing happens that way, it doesn't even recognize the click, then I changed to an hardcode way and the error happened. 但当没有任何事情发生时,它甚至不识别点击,然后我改为硬编码方式,错误发生了。 I checked my jquery include and everything seems properly included. 我检查了我的jquery包含,一切似乎都包含在内。

This is the file where I'm trying to load the comments. 这是我正在尝试加载评论的文件。 Can you find anything wrong? 你能找到什么问题吗?

I'm sorry if this is extreme newbie, but I've been stressing this way too much. 我很抱歉,如果这是一个极端的新手,但我一直在强调这种方式。

Kind regards 亲切的问候

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

It may not be the particular issue here, but the same confusing error comes up if using the "slim" version of jQuery which excludes ajax, animations, etc. If ".slim" appears in the line which includes jQuery, try taking that out. 这可能不是特定的问题,但如果使用jQuery的“瘦身”版本排除了ajax,动画等,同样会出现同样令人困惑的错误。如果“.slim”出现在包含jQuery的行中,请尝试将其取出。

Also, check the browser console to be sure jQuery is loading properly and not getting a 404 or similar error. 另外,检查浏览器控制台以确保jQuery正确加载并且没有收到404或类似错误。

Maybe change 也许改变

$("#showarea").getJSON

to

$.getJSON

May be due to jQuery version, you need to change 可能是由于jQuery版本,你需要改变

$.getJSON

to

jQuery.getJSON

Another option is droping $.getJSON and using $.ajax as (according to jQuery documentation) $.getJSON is a shorthand Ajax function, which is equivalent to: 另一个选择是删除$ .getJSON并使用$ .ajax (根据jQuery文档) $ .getJSON是一个简写的Ajax函数,它相当于:

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

you can try: 你可以试试:

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

OR 要么

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

OR 要么

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

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM