前端Get请求参数传递的方式案例一:通过?属性=属性值的方法拼接方式1: 通过?属性=属性值的方法拼接
方式2: 通过对象的方式实现数据传递
方式3: 利用restFul的结构实现参数传递.
需求:根据Id查询用户 url:url地址: http://localhost:8090/axios/findUserById
前端代码:
axios.get("http://localhost:8090/axios/findUserById?id=1")
.then(function(promise) {
console.log(promise.data)
})
案例二:通过对象的方式实现数据传递
需求:根据age/sex查询用户信息 url:http://localhost:8090/axios/findUserByAS
前端代码:
let user = {
age: 21,
sex: "女"
}
axios.get("http://localhost:8090/axios/findUserByAS", {
params: user
})
.then(function(promise) {
console.log(promise.data)
})
案例三:restful方式
需求:根据name/sex查询用户
前端代码
let name="貂蝉"
let sex="女"
let url3=`http://localhost:8090/axios/user/${name}/${sex}`
axios.get(url3).then(function(promise){
console.log(promise.data)
})
注意:url使用``(反引号)包裹
后端代码
@GetMapping("user/{name}/{sex}")
public List findUserByNS(User user){
return userService.user1(user);
}
}
Post请求select * from demo_user where name=#{name} and sex=#{sex}
params只适用于get请求方式传参
参数在http协议中传输时会变成json串
@PostMapping("/saveUser")
public String saveUser(@RequestBody User user){
System.out.println(user);
userService.saveUser(user);
return "用户入库成功!!!";
}
axios定义公共请求前缀
axios.defaults.baseURL = "http://localhost:8090"async-await 关键字
post请求练习 post请求案例



