Javascript 如何使用Node.js将Node.js中的JSON数据发送到HTML中

Javascript 如何使用Node.js将Node.js中的JSON数据发送到HTML中,javascript,jquery,json,node.js,ajax,Javascript,Jquery,Json,Node.js,Ajax,我目前正在尝试建立一个简单的在线论坛,人们可以在这里发表评论;然而,我不确定我怎么写才是正确的方法。在提交表单后,是否通过选择type=“POST”自动调用Ajax 我也不确定我是否在路由文件中编写了正确的程序 这是我的密码 <!DOCTYPE> <html lang="en"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"&

我目前正在尝试建立一个简单的在线论坛,人们可以在这里发表评论;然而,我不确定我怎么写才是正确的方法。在提交表单后,是否通过选择type=“POST”自动调用Ajax

我也不确定我是否在路由文件中编写了正确的程序

这是我的密码

<!DOCTYPE>
<html lang="en">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script>
// $(function(){
//     $("#target").submit(function(event){
//         event.preventDefault();
//         $.post("/users", function(data){
//             $( "#result" ).html( JSON.stringify(data) );
//         });
//     });
// });
//Not sure which way I should use ↑↓

$.ajax({
 type: "GET",
 url: 'http://localhost3000/users',
 data: data,
 dataType: 'json'
})

.done(function(data){
  console.log("GET Succeeded");
  $('#result').html(JSON.stringify); //What should I put after JSON.stringify?
});

 $(function(){
     $("#target").submit(function(event){
      $.ajax({
        type: "POST",
        url: 'http://localhost3000/users',
        data: data,
        dataType: 'json'
      })

      .done(function(data){
        console.log("POST Succeeded");
        $('#result').html(JSON.stringify); //What should I put after JSON.stringify?
      });
    });
 });



</script>
</head>
<body>

<div id="result"></div>

<form action="/users" method="post" id="target">
  Username:<input type="text" name="username">
  <input type="submit" value="Post">
</form>

</body>
</html>
这是我的app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

var url = 'my database';
mongoose.connect(url);

mongoose.Promise = global.Promise;

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
    // we're connected!
    console.log("Connected correctly to server");
});


var routes = require('./router/index');
var userRouter = require('./router/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/users', userRouter);
//Error handler for user
app.use(function(err, req, res, next){
  res.status(422).send({error: err.message});
});

//catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
if (app.get('env') === 'development'){
  app.use(function(err, req, res, next){
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
  });

app.use(function(err, req, res, next){
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
 });

module.exports = app;

谢谢:)

我建议使用
postman
chrome插件,它是一个restful api客户端,您首先通过它调用api,然后如果api性能完美,您可以编写ajax文章。提交表单时不会自动调用ajax文章。但是,也就是说,浏览器确实会向表单标记中的指定端点发送
post

如果需要在发表文章时自动显示注释,那么可以在脚本中使用注释代码块

另一种方法是执行以下操作

$(function(){
    $("#target").submit(function(event){
        // add code here to append the form data to the DOM

        // dont call event.preventDefault()

        // doing so will take advantage of the browser's 
        // post functionality while giving you a chance
        // to handle the form data locally prior to the post

    });
});

你指的是当表单处于焦点或单击提交按钮时,用户按下
enter
键的那一刻吗?我正在尝试建立一个论坛,所有发布的评论都已经位于评论表单上方,当用户单击提交按钮时,该评论也应该发布在其他人已经发布的所有评论下方。哦。单击submit按钮时,如果编写ajax文章,不要忘记让node server发送带有Access Control Allow Origin标头的响应,因为js中不允许跨域
$(function(){
    $("#target").submit(function(event){
        // add code here to append the form data to the DOM

        // dont call event.preventDefault()

        // doing so will take advantage of the browser's 
        // post functionality while giving you a chance
        // to handle the form data locally prior to the post

    });
});