nodejs这个过程POST求
下面是一个web登陆模拟过程。当我们问一个链接,你得到一个表格,然后填写相应的表格值,然后提交登陆。
var http = require('http'); var querystring = require('querystring'); http.createServer(function (request, response) { var responseString = ''; response.writeHead(200, {'content-type': 'text/html'}); // 假设是get请求 var postData = ""; if (request.method == "GET") { responseString = '<!doctype html><html lang="en">\ <head><meta charset="UTF-8" />\ <title>Document</title>\ </head>\ <body>\ <form action="/" method="post">\ <input type="text" name="name" value="xulidong" />\ <input type="text" name="password" value="123456" />\ <input type="text" name="code" value="abc123" />\ <input type="submit" value="submit" />\ </form>\ </body>\ </html>'; response.write(responseString); response.end(); } else if (request.method == "POST") { request.setEncoding("utf8"); request.addListener("data", function (postDataChunk) { postData += postDataChunk; }); request.addListener("end", function () { var objectPostData = querystring.parse(postData); for (var i in objectPostData) { responseString += i + " => " + objectPostData[i] + "<br>"; } response.write(responseString); response.end(); }); } }).listen(8080, '192.168.33.98');
打开浏览器输入:http://192.168.33.98:8080/,192.168.33.98是我电脑的IP,会得到如图的网页:
这个网页是我们在处理GET请求的代码中生成的,默认都填入的对应的值,我们能够改动或者直接点击submitbutton提交,然后得到例如以下的结果:
版权声明:本文博主原创文章。博客,未经同意不得转载。