国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

基于Express框架使用POST傳遞Form數(shù)據(jù)

2024-05-06 15:36:00
字體:
供稿:網(wǎng)友

本文實例為大家分享了基于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請求的代碼如下: 

var bodyParser = require('body-parser'); // ...  // create application/json parservar jsonParser = bodyParser.json() // create application/x-www-form-urlencoded parservar 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); }, });  });});            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 城步| 巫山县| 万山特区| 天祝| 麦盖提县| 贡觉县| 阜康市| 恩平市| 酉阳| 兴山县| 玉环县| 高邑县| 扎鲁特旗| 阳新县| 云安县| 英山县| 富锦市| 星座| 五莲县| 崇礼县| 定襄县| 登封市| 平罗县| 上栗县| 永州市| 桓台县| 蕉岭县| 普宁市| 德兴市| 梁平县| 吉首市| 海林市| 琼中| 石台县| 东阳市| 永平县| 揭阳市| 武义县| 微山县| 福泉市| 嵊州市|