weixin_33717117 2017-10-07 17:39 采纳率: 0%
浏览 110

Ajax调用使页面崩溃

I am trying to create a comment system with ajax. The system makes an ajax call to post_comment.php where it does a INSERT INTO and returns the info I need.

Problem: the script seems not to work.. it just freezes my page, and if I wait it refreshes the page after 8 seconds.

<script type="text/javascript">
function post()
{
  var comment = document.getElementById("content").value;
  if(comment)
  {
    $.ajax
    ({
      type: 'post',
      url: 'templates/post_comment.php',
      data: 
      {
         content:content,
         user_id:<?php echo $_SESSION['id']; ?>,
         brand_id:<?php echo $_SESSION['brand_id']; ?>,
         ticket_id:<?php echo $_GET['unique_id']; ?>
      },
      success: function (response) 
      {
          console.log('okay response');
        document.getElementById("all_comments").innerHTML=response+document.getElementById("all_comments").innerHTML;
        document.getElementById("content").value="";
        document.getElementById("username").value="";

      }, error: function() {
          alert("There was an error. Try again please!");
        }
    });
  }

  return false;
}
</script>

the only error (from google console) i see is the following: maximum call stack size exceeded jquery it appears during the refresh, and then it disappears

Any ideas? Thanks in advance!

  • 写回答

1条回答 默认 最新

  • DragonWar% 2017-10-07 18:35
    关注

    Your issue is in this line:

    content:content,
    

    Change that line to:

    content: comment,
    

    Please, change that line because content is an object and you cannot add an object inside another object. In chrome last version this object is the input content itself while in mozilla it refers to the window object.

    I report this from MDN:

    content: Deprecated This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Avoid using it and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

    I'm sure you want to refer to comment variable.

    function post(e) {
        e.preventDefault();
        var comment = document.getElementById("content").value;
        //
        // the next line in order to show the type of content
        //
        console.log('content is : ' + typeof(content));
        if (comment) {
            $.ajax({
                type: 'GET',
                url: 'https://api.github.com/repositories',
                data: {
                    since: '384',
                    //
                    // changed from content to comment
                    //
                    content: comment,
                    user_id: 'id',
                    brand_id: 'brand_id',
                    ticket_id: 'unique_id'
                },
                dataType: "json",
                success: function (response) {
                    console.log('okay response');
                    document.getElementById("all_comments").innerHTML = response + document.getElementById("all_comments").innerHTML;
                    document.getElementById("content").value = "";
                    document.getElementById("username").value = "";
    
                },
                error: function () {
                    alert("There was an error. Try again please!");
                }
            });
        }
        return false;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    
    <form>
        content: <input type="text" id="content" value="content">
        all_comments: <input type="text" id="all_comments" value="all_comments">
        username: <input type="text" id="username" value="username">
        <input type="submit" value="Submit" id="submit" onclick="post(event);">
    </form>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备