local-host 2016-01-03 20:15 采纳率: 100%
浏览 65

简单的AJAX评论系统

I'm trying to make a simple AJAX commenting system. I'm not experienced with it. Here's my code:

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>

and this is the server side node.js code for the route responsible for posting the comment and saving it to the database but it's too long and unnecessary so I'll just be putting the new code which screwed things up:

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];
}

Then I continue with my normal code which works perfectly. But the problem is with either the ajax script on the HTML or this part of the code in the route. I genuinely don't know. The error only tells me "cannot look up view error". It's an ejs error so I suppose the problem might be with the front end part. Something else that might maybe help is that the route /postcommentwritings gets me a status of 500 in the node command prompt. It doesn't get connected to. So to those who're experienced with AJAX what is the problem and how do I fix it? Thank you all in advance.

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题