溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在Express框架中使用POST傳遞Form數(shù)據(jù)

發(fā)布時(shí)間:2021-03-24 15:33:25 來(lái)源:億速云 閱讀:289 作者:Leah 欄目:web開(kāi)發(fā)

本篇文章給大家分享的是有關(guān)怎么在Express框架中使用POST傳遞Form數(shù)據(jù),小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

客戶端使用Form發(fā)送數(shù)據(jù)

在客戶端Html文件中Form代碼如下:

<!-- POST test -->
<form action="/test" method="post" id="foo" >
 <input type="text" name="username">
 <input type="password" name="password">
 <input type="submit">
</form>

服務(wù)器端處理'/test' POST請(qǐng)求的代碼如下: 

var bodyParser = require('body-parser');
 
// ... 
 
// create application/json parser
var jsonParser = bodyParser.json()
 
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
 
// ... 
 
// 
// request from form of the html file
// 
app.post('/test', urlencodedParser, function(req, res) { 
 if (!req.body) return res.sendStatus(400);
 
 console.log('Username: ' + req.body.username);
 console.log('Password: ' + req.body.password);
 
 res.send('Welcome, ' + req.body.username); 
});

客戶端使用Node.js發(fā)送數(shù)據(jù)

在客戶端使用Node.js發(fā)送Form數(shù)據(jù)的代碼

const http = require('http');
 
var data = { 
 username: 'foo', 
 password: "test" 
}; 
 
var postData = require('querystring').stringify(data); 
console.log( postData ); 
 
function form()
{
 var options = { 
 method: "POST", 
 host: "localhost", 
 port: 80, 
 path: "/test", 
 headers: { 
  "Content-Type": 'application/x-www-form-urlencoded', 
  "Content-Length": postData.length 
 } 
 }; 
 
 var body = ''; 
 var request = http.request( options, function(res) { 
 // show results 
 console.log('STATUS: ' + res.statusCode); 
 res.setEncoding('utf8'); 
 res.on('data', function(chunk) { 
  body += chunk;
  console.log('BODY: ' + chunk); 
 }); 
 
 res.on('end', function(err) { 
  console.log( ' complete.'); 
 }); 
 }); 
 request.on("error", function(e) { 
  console.log('upload Error: ' + e.message); 
 }) 
 
 request.write( postData );
 request.end(); 
}
 
form();

客戶端使用jQuery發(fā)送數(shù)據(jù)

客戶端使用jQuery的 $.ajax post數(shù)據(jù),

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>Post Data</title>
 <script src="jquery.min.js" type="text/javascript"></script>
 <script src="client.js" type="text/javascript"></script>
</head>
<body>
 
<!-- POST test -->
<form action='/update' method='post' id='foo' > Parameters
<table border="0">
 <tr>
 <td> File Name</td>
 <td> <input type="text" name="filename" value = "foo.txt" /></td>
 </tr>
</table> 
</form>
<button name="button1" id='startButton' > Update</button>
 
</body>
</html>

client.js 

$(document).ready(function(){ 
 
 //try joining the server when the user clicks the connect button
 $("#startButton").click(function () {
 var filename = $("#input[name=filename]").val();
 
 $.ajax({
 type: 'POST',
 url: "/update",
 // dataType: "jsonp",
 data: { "filename": filename} , 
 jsonpCallback: 'callback', 
 success: function (data) {
  // ...
 },
 error: function (xhr, status, error) {
  console.log('Error: ' + error.message);
 },
 });
 
 });
});

server端使用node.js解析數(shù)據(jù)

//
// Modules 
var express = require('express'); 
var bodyParser = require('body-parser'); 
 
//
// Global variables 
var app = express(); 
 
// body parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
 
 
/* POST /update listing. */
app.post('/update', function(req, res, next) {
 // Checking require
 if (!req.body) return res.sendStatus(400); 
 
 console.log('filename: ' + req.body.filename); 
 
 res.redirect('./');
});

以上就是怎么在Express框架中使用POST傳遞Form數(shù)據(jù),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI