node学习笔记(二)(ajax方式向node后台提交数据)
通过ajax向node后台提交数据过程(附手写前后台代码),并总结post与get的区别
POST
前台代码
//CSS简单给点样式
<style>
form{
width: 200px;
height: 200px;
margin: 100px auto;
}
#content{
width: 400px;
height: 60px;
border: 1px solid black;
margin: auto;
text-align: center;
line-height: 60px;
border-radius: 5px;
}
#submit{
width: 175px;
height: 30px;
background-color: skyblue;
margin: 20px 0;
border: 1px solid white;
border-radius: 5px;
}
</style>
//HTML
<body>
<div id="content">状态显示框</div>
<form action="http://127.0.0.1:8000/from" method="POST"> //post方式
<label for="login">用户名:</label><br>
<input id="login" type="text" name="user" placeholder="请输入用户名"><br> // label for属性表单绑定
<label for="login">密码:</label><br>
<input id="pwd" type="password" name="pwd" placeholder="请输入密码"><br>
<input id="submit" type="button" value="登录" onclick="loadXMLDoc()"><br>
</form>
</body>
//JS,ajax
<script>
function loadXMLDoc(){
let xhr;
let username = document.getElementsByName("user")[0];
let userpwd = document.getElementsByName("pwd")[0];
xhr = new XMLHttpRequest(); //构造一个ajax对象
xhr.onreadystatechange=function() {
if (xhr.readyState==4 && xhr.status==200) {
document.getElementById("content").innerHTML=xhr.responseText;
//接收后台数据
}
};
xhr.open("post","http://127.0.0.1:8000/from",true);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");//post必写请求头文件
xhr.send(`user=${a.value}&pwd=${b.value}`);//ES6字符串扩张之模板字符串
}
</script>
后台代码
const http = require("http");
const fs = require("fs");
const querystring = require("querystring");
const url = require("url");
//引入需要模块
let server = http.createServer(function (req,res) {
let pathname = url.parse(req.url).pathname;
if(pathname === "/" ){
fs.readFile("./login.html",function (error,data) {
if(!error){
res.writeHead(200,{"content-type":"text/html"});
res.end(data)
}else{
res.end("" + error);
}
})
}else if(pathname === "/from"){
let data = "";
req.on("data",function (chunk) {
//data事件:数据接收时...回调
data = data + chunk;//chunk参数表示data数据流不断被解析,不断发送的数据包,这个例子中数据格式为字符串,只需要一次性接收
console.log(data)//user=tom&pwd=tom,data流监听,不断接收数据流信息
});
req.on("end",function () {
//end事件:数据接收完毕时...回调
let querys = querystring.parse(data)//将data流转换为对象形式
console.log(querys);//{user:"tom",pwd:"tom"}.
if(querys.user === "tom" && querys.pwd === "tom"){
res.writeHead(200,{"content-type":"text/html;charset=utf-8"});//注意报头文一定要写
res.write("登陆成功");
res.end();
}else {
res.writeHead(200,{"content-type":"text/html;charset=utf-8"});
res.write("登陆失敗");
res.end();
}
})
}
});
server.listen(8000,"127.0.0.1",function () {
console.log("linsten")
});
GET:
前台代码:
//简单写点样式
<style>
form{
width: 200px;
height: 200px;
margin: 100px auto;
}
#content{
width: 400px;
height: 60px;
border: 1px solid black;
margin: auto;
text-align: center;
line-height: 60px;
border-radius: 5px;
}
#submit{
width: 175px;
height: 30px;
background-color: skyblue;
margin: 20px 0;
border: 1px solid white;
border-radius: 5px;
}
</style>
//HTML
<body>
<div id="content">状态显示框</div>
<form action="http://127.0.0.1:8000/from" method="GET">//提交方式为GET
<label for="login">用户名:</label><br>
<input id="login" type="text" name="user" placeholder="请输入用户名"><br>
<label for="login">密码:</label><br>
<input id="pwd" type="password" name="pwd" placeholder="请输入密码"><br>
<input id="submit" type="button" value="登录" onclick="loadXMLDoc()"><br>
</form>
</body>
//JS
<script>
function loadXMLDoc(){
let xhr;
let username = document.getElementsByName("user")[0];
let userpwd = document.getElementsByName("pwd")[0];
xhr = new XMLHttpRequest();
let url = `http://127.0.0.1:8000/from?user=${username.value}&pwd=${userpwd.value}`
//由于用ajax,get方式提交时url中不会显示具体的url,但其提交方式仍然为get,本质还是不安全,可能在url网络数据传输过程中被拦截。
xhr.open("GET",url,true);
xhr.send(null);
xhr.onreadystatechange=function() {
if (xhr.readyState==4 && xhr.status==200) {
document.getElementById("content").innerHTML=xhr.responseText;
}
console.log(xhr.responseText)
};
// xhr.open("post","http://127.0.0.1:8000/from",true);
// xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
// xhr.send(`user=${username.value}&pwd=${userpwd.value}`);
//注释掉的为POST方式请求代码
}
</script>
后台代码:
const http = require("http");
const fs = require("fs");
const url = require("url");
let sever = http.createServer(function (req,res) {
let pathname = url.parse(req.url).pathname;
let query = url.parse(req.url,true).query;//字符串转对象
if(pathname === "/"){
fs.readFile("./login.html",function (error,data) {
if(!error){
res.writeHead(200,{"content-type":"text/html"});
res.end(data);
}else {
res.end("" + error);
}
})
}else if(pathname === "/from"){
console.log(query);//对象形式
if(query.user === "tom" && query.pwd === "tom"){
res.writeHead(200,{"content-type":"text/html;charset=utf-8"});
res.write("登陆成功");
res.end();
}else{
res.writeHead(200,{"content-type":"text/html;charset=utf-8"});
res.write("登陆失敗");
res.end();
}
}
});
sever.listen(8000,"127.0.0.1",function () {
console.log("服务器已开启")
});
总结:post和get本质上都是TCP链接,是HTTP中两种发送请求的方法。,由于HTTP的规定和浏览器/服务器的限制,导致他们在应用过程中体现出一些不同。
区别一:最直观的区别就是GET把参数包含在URL中,POST是通过request body 传送参数,“request body”就是请求文件返回的内容。
区别二:get传送的数据量较小,不能大于2KB。(取决于浏览器本身),post传送的数据量较大,一般被默认为不受限制。可以上传图片、文件等非字符串格式数据。
区别三:因为GET参数通过URL传递,那么GET方式不是安全的,这里的安全指:比如账号密码登录用GRT,别人可以通过URL看到你的信息,尽管通过ajaxURL地址栏不会显示,但是仍然可以用firefox或者其他的软件看到请求内容,实质上还是会通过URL网络传输被截取到。
区别四:GET是“安全“的,这里的安全指的是不会改变服务器端的数据,只会接收请求,而POST则可以改变服务器上资源的请求。
今天的学习笔记就到这里,纯个人手写,如有发现错误,欢迎指出,非常感激!
node学习笔记(二)(ajax方式向node后台提交数据)的更多相关文章
- node学习笔记(二)
process.stdout(); //标准输出流 process.stdout.write() //提供了比console.log更底层的接口 process.stdin(); //标准输入流 // ...
- node学习笔记(二)流和缓冲区
内容 视频 第四章内容 菜鸟教程服务器 //复制文件 function de(x) { console.log(x); } var fs=require('fs'); fs.mkdir('stuff' ...
- Nodejs学习笔记(十五)--- Node.js + Koa2 构建网站简单示例
目录 前言 搭建项目及其它准备工作 创建数据库 创建Koa2项目 安装项目其它需要包 清除冗余文件并重新规划项目目录 配置文件 规划示例路由,并新建相关文件 实现数据访问和业务逻辑相关方法 编写mys ...
- [转]Nodejs学习笔记(十五)--- Node.js + Koa2 构建网站简单示例
本文转自:https://www.cnblogs.com/zhongweiv/p/nodejs_koa2_webapp.html 目录 前言 搭建项目及其它准备工作 创建数据库 创建Koa2项目 安装 ...
- Nodejs学习笔记(十五)—Node.js + Koa2 构建网站简单示例
前言 前面一有写到一篇Node.js+Express构建网站简单示例:http://www.cnblogs.com/zhongweiv/p/nodejs_express_webapp.html 这篇还 ...
- AJax 学习笔记二(onreadystatechange的作用)
AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...
- node学习笔记第一天
ES6---* JavaScript语言随着使用的人越来越多,ECMA语法规范:if/else* 为了让js语言更适应大型应用的开发.旨在消除一些怪异的行为 ### 包含内容(strict严格模式)- ...
- Node.js学习笔记(1):Node.js快速开始
Node.js学习笔记(1):Node.js快速开始 Node.js的安装 下载 官方网址:https://nodejs.org/en/ 说明: 在Windows上安装时务必选择全部组件,包括勾选Ad ...
- Linux内核学习笔记二——进程
Linux内核学习笔记二——进程 一 进程与线程 进程就是处于执行期的程序,包含了独立地址空间,多个执行线程等资源. 线程是进程中活动的对象,每个线程都拥有独立的程序计数器.进程栈和一组进程寄存器 ...
随机推荐
- win10 uwp 获取指定的文件 AQS
很多时候不需要获取整个文件夹的文件,是需要获取文件夹里指定的文件. 那么 UWP 如何对文件夹里的文件进行过滤,只拿出自己需要的文件? 本文:如何使用通配符或文件匹配方式在uwp获取文件夹中指定的文件 ...
- Ubuntu下使用网易云音乐
Ubuntu15真心各种崩溃啊 最后决定还是换成ubuntu14.04LTS了 在win.android平台上网易云音乐好用到爆 ubuntu下没有网易云音乐的客户端怎么能行 https://gith ...
- 表单处理的方案与注意事项(servlet)
摘要 表单是后端程序员用的与接触最多的,我这里例举了常用处理办法,与注意事项 sevlet处理代码 package myform; import java.io.IOException; import ...
- 2年Java开发工作经验面试总结
最近换了个公司,从三月底开始面,面到四月底,面了有快二十家公司.我是一个喜欢总结经验的人,每经过一场面试,我在回来的路上都会仔细回想今天哪些问题可以答的更好,或者哪些问题是自己之前没遇到过的,或者是哪 ...
- NOIP2012疫情控制(二分答案+倍增+贪心)
Description H国有n个城市,这n个城市用n-1条双向道路相互连通构成一棵树,1号城市是首都,也是树中的根节点. H国的首都爆发了一种危害性极高的传染病.当局为了控制疫情,不让疫情扩散到边境 ...
- JAVA中HashMap和Hashtable区别
Hashtable和HashMap在Java面试中相当容易被问到,甚至成为了集合框架面试题中最常被考的问题,所以在参加任何Java面试之前,都不要忘了准备这一题. 我们先看2个类的定义 public ...
- Markdown不常见功能
推荐几个Markdown不常见功能 1.表情符号 emoji表情使用:EMOJICODE:的格式,详细列表可见 https://www.webpagefx.com/tools/emoji-cheat- ...
- Linux.SSH.修改SSH端口号
Linux系统的默认SSH端口是22, 一般为发安全起见, 建议修改成其它端口 编辑配置文件: vi /etc/ssh/sshd_config 找到 #Port 22 把前面的#号去掉, 22修改成新 ...
- 【20171027中】alert(1) to win 第13,14,15,16题
第13题 题目: function escape(s) { var tag = document.createElement('iframe'); // For this one, you get t ...
- python 使用标准库连接linux实现scp和执行命令
import stat import pexpect 只显示关键代码: sqldb = localpath+database //获取database名字 if os.path.exists(sqld ...