node如何获取参数以及ajax如何请求参数
故心故心故心故心小故冲啊
文章目录
前言
每天进步一点点.......一、 get请求参数通过url?name=xx&age=xx&sex=xx访问 node获取参数为req.query
// get请求参数通过url?name=xx&age=xx&sex=xx访问 node获取参数为req.query
function send1() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("get", "http://localhost:3000/data/query?name=xm&age=18&sex=nan");
xmlhttp.send(); //发送
xmlhttp.onreadystatechange = function () { //接收数据
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText);
content.innerHTML = xmlhttp.responseText;
}
}
}
//接口实现
//http://localhost:3306/
//req request(请求) res response(响应)
// 请求参数通过url?name=xx&age=xx&sex=xx访问 获取参数为req.query
app.get('/data/query',function(req,res){
console.log(req.query); //
res.send(JSON.stringify(data));
})
二、post请求参数通过send({})访问 post请求需要设置请求头 node获取参数为req.body
//post请求参数通过send({})访问 post请求需要设置请求头 node获取参数为req.body
function send2(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("post", "http://localhost:3000/data/add", true);
//指定发送的内容格式
xmlhttp.setRequestHeader("Content-Type","application/json");
xmlhttp.send(JSON.stringify({"name":"xm", "age":18}));
xmlhttp.onreadystatechange = function() { //接收数据
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText)
//把拿到的数据展示
content.innerHTML = xmlhttp.responseText;
}
}
}
//接口实现
//http://localhost:3306/
//req request(请求) res response(响应)
//post请求 获取参数为req.body
app.post('/data/add',function(req,res){
console.log(req.body);
res.send(JSON.stringify(data));
})
三、get请求通过url/参数传参 node获取参数为req.param
// get请求通过url/参数传参 node获取参数为req.param(node规定了data/后面就是参数)
function send3(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("get", "http://localhost:3000/data/xm");
xmlhttp.send(); //发送
xmlhttp.onreadystatechange = function() { //接收数据
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText)
//把拿到的数据展示
content.innerHTML = xmlhttp.responseText;
}
}
}
//接口实现
//http://localhost:3306/
//req request(请求) res response(响应)
// 规定了data/后面就是参数 获取参数为req.param
app.get('/data/:name',function(req,res){
console.log(req.param); //
res.send(JSON.stringify(data));
})
})
四、 规定formdata数据格式发送请求 发送的数据通过append加入
//规定formdata数据格式发送请求 发送的数据通过append加入
//node获取参数为 form.parse(req,function(fields)) 通过fields获取传过来的参数
function send4(){
var xmlhttp = new XMLHttpRequest();
//formdata 不要配置请求头
xmlhttp.open("post", "http://localhost:3000/data/form", true);
var formdata = new FormData();
formdata.append("name", "xm");
formdata.append("age", 18)
xmlhttp.send(formdata);
xmlhttp.onreadystatechange = function() { //接收数据
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText)
//把拿到的数据展示
content.innerHTML = xmlhttp.responseText;
}
}
}
//接口实现
//http://localhost:3306/
//req request(请求) res response(响应)
//formdata数据格式 获取参数为fields
app.post("/data/form", function(req, res) {
var form = new multiparty.Form();
form.parse(req, function(err, fields) {
console.log( fields)
res.send(JSON.stringify(fields));
})
})
总结
1.url?=>req.query2.url/ =>req.param
3.post请求 =>req.body
4formdata =>form.parse(req, function(err, fields) { console.log( fields) })