Nodejs express获取Ajax(post方式)发送的数据
2017-09-09 11:12
465 查看
提供两种常用方法
一,利用req.on(‘data’,function(data){})读取发送至服务端数据内容
前端代码(index.html)
nodejs代码(app.js)
运行结果
二、利用中间件bodyParser
前端代码(index.html)
nodejs代码(app.js)
运行结果和 一 相同
一,利用req.on(‘data’,function(data){})读取发送至服务端数据内容
前端代码(index.html)
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form action="" > 名称:<input type="text" name='name'/><br> 密码:<input type="password" name='password' /><br> <input type="button" onclick=''/> </form> <p id='msg'></p> <script type="text/javascript"> function Ajax(json){ var url=json.url; var method=json.method; var success=json.success; var obj=json.obj; var request=null; if(window.XMLHttpRequest){ request=new XMLHttpRequest(); }else{ try{ request=new ActiveObject('Microsoft.XMLHTTP'); } catch(faild){ alert('Error:Ajax request faild'); } } if(request!=null){ request.onreadystatechange=function(){ if(request.readyState==4&&request.status==200){ var text=request.responseText; success(text); }else{ } } request.open(method,url,true); request.send(JSON.stringify(obj)); } } function post=function(){ obj={ name:document.getElementsByName('name')[0].value, password:document.getElementsByName('password')[0].value }; Ajax({ url:'/', method:'POST', success:function(text){ document.getElementById('msg').innerText=text; }, obj:obj }) } </script> </body> </html>
nodejs代码(app.js)
var express=require('express'); var app=express(); app.get('/',function(req,res){ res.sendfile(__dirname+'/index.html') }) app.post('/',function(req,res){ req.on('data',function(data){ obj=JAON.parse(data); console.log(obj); res.send('数据已接收') }) }) app.listion(3000,'localhost');
运行结果
二、利用中间件bodyParser
前端代码(index.html)
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form action="" id='form'> 名称:<input type="text" name='name'/><br> 密码:<input type="password" name='password' /><br> <input type="button" value='确定' onclick='post()'/> </form> <p id='msg'></p> <script type="text/javascript"> function Ajax(json){ var url=json.url; var method=json.method; var success=json.success; var formData=json.formData; var request=null; if(window.XMLHttpRequest){ request=new XMLHttpRequest(); }else{ try{ request=new ActiveObject('Microsoft.XMLHTTP'); } catch(faild){ alert('Error:Ajax request faild'); } } if(request!=null){ request.onreadystatechange=function(){ console.log(request.readyState) if(request.readyState==4&&request.status==200){ var text=request.responseText; success(text); }else{ } } request.open(method,url,true); request.send(formData); } } function post(){ var formData=new FormData(document.getElementById('form')) Ajax({ url:'/', method:'POST', success:function(text){ document.getElementById('msg').innerText=text; }, formData:formData }) } </script> </body> </html>
nodejs代码(app.js)
var express=require('express'); var app=express(); app.use(express.bodyParser())//解析请求的body中的内容[必须] app.get('/',function(req,res){ res.sendfile(__dirname+'/index.html') }) app.post('/',function(req,res){ console.log(req.body); res.send('数据已接收') }) app.listen(3000,'localhost');
运行结果和 一 相同
相关文章推荐
- 获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
- 通过ajax引擎对象向服务器发送数据的2中方式:get、post
- 获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
- NodeJS中,Express获取request所传递的数据方式
- 获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
- http_post_data发送数据的获取方式
- nodejs 的ajax获取数据express
- 使用post方式提交表单如何获取图片数据及其他文本参数[NodeJS]
- (转)获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
- ajax请求当发送post方式application/json格式数据,url后面又带有参数的时候
- express 4.x req.query(get方式) req.body(post方式) 接收浏览器发送过来的数据
- 基于Jsoup的爬虫中ajax获取数据的方式(post)
- http的post方式连接服务器,发送数据到服务端,并获取服务端的数据
- 获取 request 中用POST方式"Content-type"是"application/json"发送的 json 数据
- ashx文件获取$.ajax()方法发送的数据
- 跨域获取数据ajax方式
- nodejs 获取post数据
- C#模拟Post和Get方式发送数据的技巧
- C#模拟Post和Get方式发送数据的技巧
- 怎样获取form-data方式POST的数据