2017-10-07 109 views
1

我想创建一个带有Ajax的评论系统。系统对post_comment.php进行ajax调用,在那里执行INSERT INTO并返回我需要的信息。Ajax调用崩溃页面

问题:脚本似乎不工作..它只是冻结我的页面,如果我等待它刷新页面8秒后。

<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> 

(从谷歌控制台)唯一的错误我看到的是如下:最大调用堆栈大小超过了jQuery的 它在刷新过程中出现,然后消失

任何想法?提前致谢!

+0

为什么'返回false;'这剂有什么影响? – Webdesigner

+0

你可以添加你的html代码吗? @Webdesigner我认为这是为了避免在刷新页面 – linasmnew

+1

时触发'post()'?用户点击按钮时是否是这样?你得到的错误意味着你超出了浏览器内存的限制 –

回答

1

你的问题是在这条线:

content:content, 

更改该行:

content: comment, 

请更改线路,因为内容是一个对象,你不能添加内的另一个对象目的。在铬最后的版本这个对象是输入内容本身,而在Mozilla它是指窗口对象。

我从MDN报告此:

content:不赞成 此功能已经从Web标准中删除。尽管一些浏览器可能仍然支持它,但它正在被丢弃。避免使用它并尽可能更新现有的代码;请参阅本页底部的兼容性表格来指导您的决定。请注意,此功能可能随时停止工作。

我确定你想参考评论变量。

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>

+0

yess !!那就是问题......但不是因为你说了什么...只是因为我用了另一个名字来表示变量我想用..对不起:) – Nic