简单的AJAX评论系统

时间:2016-01-03 20:15:25

标签: javascript ajax node.js ejs

我正在尝试制作一个简单的AJAX评论系统。我没有经验。这是我的代码:

function sendComment() {
  var obj = {       //make an object to send via ajax
    values: [document.forms['commentform']['getpage'].value, document.forms['commentform']['commentname'].value, document.forms['commentform']['writingcomment']]
  };
  if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
    xhr = new XMLHttpRequest();
  } else { // code for IE6, IE5
    xhr = new ActiveXObject('Microsoft.XMLHTTP');
  }

  xhr.open('POST', '/postcommentwritings', true);
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.send(JSON.stringify(obj));  //stringify the object to send via ajax
}
<div class="comments-section">Comments</div> <a id="reader-delete-btn" class="menu-reader-viewer">&hellip;</a>
<div class="comments-container">
  <div class="comments-container-child">
    <form name="commentform" onsubmit="return validatecomm()" method="POST">
      <input type="text" name="commentname" placeholder="Optional: give yourself a name">
      <br>
      <textarea name="writingcomment" id="writeComment" placeholder="comment.."></textarea>
      <input type="hidden" name="getpage" value=<%=story.ID %>>
      <input type="submit" onclick="sendComment()" value="submit">
    </form>
  </div>
</div>

这是负责发布评论并将其保存到数据库的路由的服务器端node.js代码,但它太长且没必要,所以我只是把新代码搞砸了:

router.post('/postcommentwritings', function(req, res){

var commentsObj = {
    post_ID : false,
    name: "",
    comment: false 
}

var getObj= JSON.parse(obj);
for(x in getFoo){
    commentsObj['post_ID'] = getObj[x][0];
    commentsObj['name'] = getObj[x][1];
    commentsObj['comment'] = getObj[x][2];
}

然后我继续使用我的正常代码,它完美无缺。但问题在于HTML上的ajax脚本或路由中的这部分代码。我真的不知道。该错误只告诉我“无法查看查看错误”。这是一个ejs错误,所以我想问题可能出在前端部分。其他可能有帮助的是路由/postcommentwritings在节点命令提示符下使我的状态为500。它没有连接到。那些对AJAX有经验的人有什么问题,我该如何解决?提前谢谢大家。

0 个答案:

没有答案
相关问题