簡體   English   中英

如何通過ajax將表單數據發送到nodejs服務器

[英]How to send form data by ajax to nodejs server

我創建了一個 html 表單,我想使用 ajax 將數據傳遞給 nodejs 服務器,服務器在成功上傳后返回確認消息。

<form action="http://localhost:8080/task" method="POST" >
    <div class="row">
        <div class="col-md-3"></div>
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h3 class="custom-title">Form</h3>
                </div>
                <div class="card-body">
                    <div class="form-group">
                        <label for="appIdNameCombo">appIdNameCombo :</label>
                        <input type="text" name="appIdNameCombo" class="form-control" id="appIdNameCombo" required>
                    </div>
                    <div class="form-group">
                        <label for="fileLocation">fileLocation :</label>
                        <input type="text" name="fileLocation" class="form-control" id="fileLocation" required>
                    </div>
                    <div class="form-group">
                        <label for="assetList">assetList :</label>
                        <input type="text" name="assetList" class="form-control" id="assetList" required>
                    </div>
                    <div class="form-group">
                        <br>
                        <button type="submit">submit</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </form>

如果您使用ajax,請不要使用表單方法發送數據。 請改用 axios。 你只需要寫下來

axios.post(http://localhost:8080/task, {yourdata}).then(res => {
  console.log(res.data)
});

在 ajax jquery 中使用該代碼。 如果你不知道axios,搜索它,它被廣泛使用。

在節點中,使用正文解析器添加它

const bodyParser = require("body-parser");

const app = express();

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

暫無
暫無

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

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