node如何获取参数以及ajax如何请求参数

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.query
2.url/ =>req.param
3.post请求 =>req.body
4formdata =>form.parse(req, function(err, fields) { console.log( fields) })
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在前端使用Vue框架,我们可以通过上传文件的方式获取用户选择的头像图片。可以使用input[type="file"]元素或者第三方的Vue插件来实现。确认用户选择了头像图片后,我们可以使用FormData对象来构建一个表单,将头像图片作为一个文件参数添加到表单中。 下来,通过AJAX请求将表单数据发送给后端的Node.js服务器。可以使用Vue的axios库来发送AJAX请求。在请求配置中,设置请求的URL和请求方法为POST。 在Node.js服务器中,我们可以使用Multer中间件来处理从前端传来的文件数据。Multer中间件会将上传的文件保存到服务器的磁盘上,并将文件信息作为请求对象的一个属性进行传递。 在请求到达服务器后,将文件保存到合适的位置,例如存储在服务器的文件夹中。可以使用Node.js的fs模块来实现文件的保存。同时,将文件的存储路径和其他需要保存到数据库的用户信息一起传递给MySQL数据库。 使用Node.js的mysql模块,连到MySQL数据库并执行相应的数据库操作,将用户的头像图片的存储路径和其他用户信息存储到数据库中的对应表中。可以使用INSERT语句来插入数据。 需要确保MySQL数据库已经创建了合适的表,并且字段的类型和长度与存储的数据匹配。 最后,返回相应的结果给前端,告知用户头像图片上传的成功与否。 需要注意的是,以上仅是一个大致的流程和步骤示例,具体实现方式会根据具体的需求和情况而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值