簡體   English   中英

表單未在節點中提交

[英]Form is not submitting in node

我正在嘗試從 html 表單發出發布請求,但無法弄清楚我哪里出錯了。

>         <form action="/api" method="POST">
            <label for="username">username or email address</label>
            <input name="username" id="username" type="text">
            <label for="password">password</label>
            <input id="password"name="password" type="text">
            <button >Log in</button>
        </form>

這是我的 html(不是服務器)的主要 javascript 文件

    "use strict"

let options = {
    headers:{
        "Content-Type" : "application/json"
    },
    method: "POST",
}
// fetch("/api",options)

這是我的節點 js 服務器

"use strict"

//Installing express
let express = require(`express`)
let app = express()
app.use(express.json())

//running the server
app.listen(3000,()=>{
    console.log("server is running boi");
})
//Middleware to load the static content
app.use(express.static(`public`))

//Database stuff 
let Datastore = require('nedb')
let db = new Datastore({ filename: 'database.db' });
db.loadDatabase()
db.insert({username:"sid", password:"westham"})

//Handler for any post requests made
app.post(`/api`,(req,res)=>{
    console.log("request was made");
    console.log(req.body);
    
})

兩個觀察

server.js文件中找不到用於處理表單數據的middleware ,請使用body-parser http://expressjs.com/en/resources/middleware/body-parser.html

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true });

在您的 html 表單中,如果您未使用 JavaScript 提交表單,則將按鈕類型稱為submit

<button type="submit" >Log in</button>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM