将HTML FormData发送到Node.js服务器时出错

时间:2020-11-08 12:57:35

标签: html node.js express

我在不同的端口上使用html表单和nodejs服务器。每次我尝试将数据从HTML表单发送到nodejs服务器时,都会出现错误405。但是,如果我在同一端口上运行它们,则一切正常。 这是我的htmlform代码。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="style.css">
    <title>HTML FORM</title>
</head>

<body>
    <div class="container">
        <form action="http://localhost:3000/register" method="POST">
            <div class="element">
                <label for="name">NAME</label>
                <input type="text" name="name" id="name" autocomplete="on">
            </div>
            <div class="element"> 
                <label for="RollNo">ROLL NUMBER</label>
                <input type="tel" name="RollNo" id="RollNo">
            </div>
            <div class="element">
                <label for="contact">MOBILE</label>
                <input type="tel" name="contact" id="contact">
            </div>
            <div class="element">
                <label for="branch">BRANCH</label>
                <select name="branch" id="branch">
                    <option value="CSE">CSE</option>
                    <option value="ME">ME</option>
                    <option value="ECE">ECE</option>
                    <option value="CIVIL">CIVIL</option>
                </select>
            </div>
            <div class="element">
                YEAR <br>
                <input type="radio" name="Year" id="First">
                <label for="First">1st Year</label><br>
                <input type="radio" name="Year" id="Second">
                <label for="Second">2nd Year</label><br>
                <input type="radio" name="Year" id="Third">
                <label for="Third">3rd Year</label><br>
                <input type="radio" name="Year" id="Fourth">
                <label for="Fourth">4th Year</label>
            </div>
            <div class="element">
                <label for="DOB">Date of Birth</label>
                <input type="date" name="DOB" id="DOB">
            </div>
            <div class="element">
                <label for="email">Email</label>
                <input type="email" name="email" id="email">
            </div>
            <div class="element">
                <label for="password">Password</label>
                <input type="password" name="password" id="password">
            </div>
            <!-- <div class="element">
                <label for="image">Profile Photo</label>
                <input type="file" name="image" id="image">
            </div> -->
            <div class="element">
                <input type="submit" name="submit" id="submit">
                <input type="reset" name="reset" id="reset"><br>
            </div>
        </form>

    </div>

</body>

</html>

而nodejs服务器的代码是这个

const express=require('express');
const bodyParser=require('body-parser');

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:false}))

app.post('/register',(req,res)=>{
    console.log(req.body);
    res.send(req.body);
})


app.listen(3000);

在这种情况下,我的HTML表单在端口8080上运行,服务器在端口3000上运行。

1 个答案:

答案 0 :(得分:0)

您是否尝试过查看CORS NPM软件包?

在server.js中:

const cors = require('cors')
app.use(cors());