目录

详解axios四种传参,后端接参

详解axios四种传参,后端接参

详解axios四种传参,后端接参

方式一、通过data字段将json数据发送到后台(用",“发送数据)

//格式:axios.delete(服务器链接,{data:{键:值}})

axios.delete("/delete" , {data:{aid:row.aid}}).then(resp => {
    let resultInfo = resp.data;
    
})

前端浏览器发送的数据

https://i-blog.csdnimg.cn/blog_migrate/eea0846765a0951ba55b213642a0e882.png

后端接参

@RequestBody 指定接收的是 json 格式的参数,然后参数类型是 Map类型 ,通过map的键取出数据。

/* 格式:
		@DeleteMapping("/delete")
		@RequestBody Map<String,前端发送的值的数据类型> 形参名
*/

@DeleteMapping("/delete")
public ResultInfo delete(@RequestBody Map<String,Object> params) {
    System.out.println(params.get("aid"));
    return new ResultInfo(true);
}

后端服务器接收的数据:{aid=11}

方式二、通过params字段将json数据发送到后台(用”,“发送数据),实际上是在网址上做拼接。

// 格式:axios.delete(服务器链接,{params:{键:值}})

axios.delete("/delete" , {params:{aid:row.aid}}).then(resp => {
    let resultInfo = resp.data;
    
})

前端浏览器发送的数据

https://i-blog.csdnimg.cn/blog_migrate/e9fedc902a6b4fd79b130e0f9b7841d5.png

后端接收:

@RequestBody 指定接收的是 json 格式的参数,然后参数可以 通过名字自动匹配

@DeleteMapping("/delete")
public ResultInfo delete(Integer aid) {
	System.out.println(aid);
    return new ResultInfo(true);
}

参数名不一致 则用 @RequestParam(“前端传过来的参数名”) 做匹配

@DeleteMapping("/delete")
public ResultInfo delete(@RequestParam("aid") Integer aaid) {
    System.out.println(aaid);
    return new ResultInfo(true);
}

后端服务器接收的数据:107

方式三、直接在地址栏上拼接数据,且必须在拼接前加”/"

//格式:axios.delete("/服务器链接/"+值)

axios.delete("/delete/"+row.aid).then(resp => {
    let resultInfo = resp.data;
    if (resultInfo.success) {
        this.$message({
            type: 'success',
            message: resultInfo.message
        });
    }
})

客户端

https://i-blog.csdnimg.cn/blog_migrate/d5792b9c8132c4646a6999fe673d7001.png

后端接收(服务器)

// 通过在 /{参数名} 得到访问方法,然后取值用@PathVariable("参数名")

@DeleteMapping("/delete/{aid}")
public ResultInfo delete(@PathVariable("aid") Integer aid) {
    System.out.println(aid);
    return new ResultInfo(true);
}

https://i-blog.csdnimg.cn/blog_migrate/be78f2cc7a392e00a941642fef57d874.png

方式四、传统方式?参数名=参数值

axios.delete("/delete?aid="+row.aid).then(resp => {
    let resultInfo = resp.data;
})

https://i-blog.csdnimg.cn/blog_migrate/d6be32af289096c31abbf0aba5ee4767.png

服务器直接接收, 按名字匹配 ,如果名字不一致,则用 @RequestParam 进行匹配

@DeleteMapping("/delete")
public ResultInfo delete(Integer aid) {
    System.out.println(aid);
    return new ResultInfo(true);
}

https://i-blog.csdnimg.cn/blog_migrate/a44d6bdaf2841084951beb48ed12beb7.png

注: get和delete几乎完全一致