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 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?