【问题标题】:My POST from html does not return any value, nodejs, express我来自 html 的 POST 不返回任何值,nodejs,express
【发布时间】:2015-06-16 11:24:52
【问题描述】:

我正在尝试从 html 页面做一个简单的帖子,当我在帖子之后检查控制台时,它什么也没返回。我在服务器端正确使用了正文解析器。有什么我做错了吗?

   var http = require('http');
var fs = require('fs');
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
//var routes = require('./routes');

//****** Mongodb Code *********************************************
var mongodb = require('mongodb');
var Db = require('mongodb').Db;
var assert = require('assert');
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
var url = 'mongodb://localhost:27017/test';
var mongoose = require('mongoose');
//*****************************************************************

var app = express();
/*
var index = fs.readFileSync('index.html')
var signin = fs.readFileSync('signin.html')
var createaccount = fs.readFileSync('createaccount.html')
var forgotpassword = fs.readFileSync('forgotpassword.html')
var account = fs.readFileSync('account.html')
var upload = fs.readFileSync('uploadphoto.html')
*/

/*
var serv = http.createServer(function (req, res){
    res.writeHead(200, { 'Content-Type': 'text/html; char=utf-8'});
    res.end(index);
})
*/


//  app.set('views',__dirname + '/views');
    app.set('view engine', 'ejs');
    app.use(express.static(path.join(__dirname)));
    app.use(bodyParser.json())

app.get("/forgotpassword", function(req, res){
    res.render('forgotpassword');
})

app.post("/forgotpassword", function(req,res){
    console.log(req.body);
})

app.listen(3000);

    console.log("Express server listening on port 3000.")

下面是我的html代码。

<body>

<div id="header">
    <div id="link">
    <a href="/" align="right">Home</a>
    <a align="right" href="signin">Sign-in</a>
    </div>
    <div id="logo">
    <center><h1><i>IMGCAPTURE</i></h1></center>
    </div>
</div>

<div id="forgotpassword">
    <form id="enteremail" action="forgotpassword" method="POST">
    <center><h1><i>Enter Your Email:</i></h1></center><br><br>
    <center><input id="useremail" name="email" type="email"></input></center><br>
    <center><input type="submit" value="Submit"></input></center>
</form>
</div>
    </body>
    </html>

【问题讨论】:

    标签: html node.js express


    【解决方案1】:

    尝试使用

    app.use(bodyParser.urlencoded({ extended: true }));
    

    而不是

    app.use(bodyParser.json())
    

    【讨论】:

      猜你喜欢
      • 2017-06-19
      • 2018-03-12
      • 2018-12-23
      • 2018-01-14
      • 2020-04-04
      • 1970-01-01
      • 2013-11-14
      • 2019-08-27
      • 1970-01-01
      相关资源
      最近更新 更多