基于Express框架使用POST傳遞Form數(shù)據(jù)
本文實(shí)例為大家分享了基于Express框架使用POST傳遞Form數(shù)據(jù)的具體代碼,供大家參考,具體內(nè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('./'); });
參考文獻(xiàn):expressjs/body-parser
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript基礎(chǔ)語(yǔ)法之js表達(dá)式
這篇文章主要介紹了JavaScript基礎(chǔ)語(yǔ)法之js表達(dá)式 的相關(guān)資料,需要的朋友可以參考下2016-06-06javascript實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊頁(yè)面 移動(dòng)DIV
本篇文章主要介紹javascript實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊頁(yè)面,移動(dòng)DIV。話不多說(shuō),附上代碼實(shí)例。需要的朋友來(lái)看下吧2016-12-12JavaScript中的定時(shí)器之Item23的合理使用
window對(duì)象提供了兩個(gè)方法來(lái)實(shí)現(xiàn)定時(shí)器的效果,分別是window.setTimeout()和window.setInterval。其中前者可以使一段代碼在指定時(shí)間后運(yùn)行;而后者則可以使一段代碼每過(guò)指定時(shí)間就運(yùn)行一次2015-10-10把多個(gè)JavaScript函數(shù)綁定到onload事件處理函數(shù)上的方法
下面小編就為大家?guī)?lái)一篇把多個(gè)JavaScript函數(shù)綁定到onload事件處理函數(shù)上的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09非常不錯(cuò)的一個(gè)JS分頁(yè)效果代碼,值得研究
非常不錯(cuò)的一個(gè)JS分頁(yè)效果代碼,值得研究...2007-06-06