今天在学习elasticsearch时,遇到一个问题:项目中前端采用的是Vue2+axios,后端的接口采用Restful风格来接收:

关于Resultful风格:

  1. GET(SELECT):从服务器取出资源(一项或多项);

  2. POST(CREATE):在服务器新建一个资源;

  3. PUT(UPDATE):在服务器更新资源(客户端提供改变后的完整资源);

  4. PATCH(UPDATE):在服务器更新资源(客户端提供改变的属性);

  5. DELETE(DELETE):从服务器删除资源;

  6. HEAD:获取资源的元数据;

  7. OPTIONS:获取信息,关于资源的哪些属性是客户端可以改变的。

前端代码
//axios.post("/hotel/list", params)//这是采用的post方式(刚开始是这样的,我将post改为get后,便会报错)
axios.get("/hotel/list",{//这是采用get方式,这两种方式最后结果都正确,后面会说具体怎么做的
params:params
})
.then(resp => {
this.hotels = resp.data.hotels;
this.total = resp.data.total;
this.totalPage = Math.floor((this.total + 5 - 1) / 5);
if (location) {
this.setMapCenter(location);
} else if(this.hotels && this.hotels.length > 0){
this.setMapCenter(this.hotels[0].location);
}
this.initMarker();
})
.catch(err => {
console.log(err)
this.hotels = []
})

  可以看到一般要查询数据时是采用GetMapping接收,但是在学习的源码中发现,查询时向后端的请求时POST方式,后端也是PostMapping接收参数,当我将POST方法改为GET方法时又会报空指针的错误(前端传递的参数后端没有接收到)为什么改个传递方式后,结果就传不进去了?随后我便查找了axios的官网

请求方式别名
为了方便起见,已经为所有支持的请求方法提供了别名。 axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.options(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])

  可以看到,axios.post("/hotel/list", params),Post方法是可以直接传参的,但是Get方法却不能直接传参,需要在配置中(config)配置。

  具体的传递参数方法案例如下:

一、Get请求

  1.1基础类型接收,名字对应就可以

// method
const params = {
id: '123456789',
name: '张三'
}
test(params) // api
export function test (params) {
return axios({
url: url,
method: 'GET',
params: params
})
} // 后台
@GetMapping("/test")
public Result test(Long id, String name) {
return Res.ok();
}

  1.2使用Map接收,需要添加 RequestParam 注解

// method
const params = {
id: '123456789',
name: '张三'
}
test(params) // api
export function test (params) {
return axios({
url: url,
method: 'GET',
params: params
})
} // 后台
@GetMapping("/test")
public Result test(@RequestParam Map<String, Object> map) {
return Res.ok();
}

  1.3实体类接收

// 实体类
@Data
public class TestEntity {
Long id;
String name;
} // method
const params = {
id: '123456789',
name: '张三'
}
test(params) // api
export function test (params) {
return axios({
url: url,
method: 'GET',
params: params
})
} // 后台
@GetMapping("/test")
public Result test(TestEntity testEntity) {
return Res.ok();
}

  二、Post请求

  2.1基础类型接收,名称对应即可

// method
const params = {
id: '123456789',
name: '张三'
}
test(params) // api
export function test (params) {
return axios({
url: url,
method: 'POST',
params: params
})
} // 后台
@PostMapping("/test")
public Result test(Long id, String name) {
return Res.ok();
}

  2.2使用map接收

// method
const params = {
id: '123456789',
name: '张三'
}
test(params) // api
export function test (params) {
return axios({
url: url,
method: 'POST',
params: params
})
} // 后台
@PostMapping("/test")
public Result test(@RequestParam Map<String, Object> map) {
return Res.ok();
}

  2.3使用实体类接收(params传递参数)

// 实体类
@Data
public class TestEntity {
Long id;
String name;
} // method
const params = {
id: '123456789',
name: '张三'
}
test(params) // api
export function test (params) {
return axios({
url: url,
method: 'POST',
params: params
})
} // 后台
@PostMapping("/test")
public Result test(TestEntity testEntity) {
return Res.ok();
}

  2.4实体类接收(data传递参数)

// 实体类
@Data
public class TestEntity {
Long id;
String name;
} // method
const params = {
id: '123456789',
name: '张三'
}
test(params) // api
export function test (params) {
return axios({
url: url,
method: 'POST',
data: params
})
} @PostMapping("/test")
public Result test(@RequestBody TestEntity testEntity) {
return Res.ok();
} 

总结
总体来说,只要使用 params get与post请求基本是一样使用的,如果参数名与传递名称不一致,需要使用@RequestParam修饰.

若使用Map接收参数,必须使用@RequestParam修饰。但是如果想传list类型的数据,需要使用单独的方法处理。

若使用data传递参数,必须使用一个实体类接收参数,而且需要添加注解@RequestBody进行修饰

  

 

axios传递参数的使用的更多相关文章

  1. 解决axios传递参数后台无法接收问题

    1.根据下面几个方法改变前台传递参数方式 这样后台就可以直接根据传递的参数获取数据,如下图用户登录时直接传递用户名和密码 2.不改变前台传递样式修改后台接收方式

  2. axios以form-data形式的传递参数遇到的坑

    axios默认的Content-type是application/json;charset=UTF-8,如果想要以表单的形式传递参数,只要修改{headers:{'Content-Type':'app ...

  3. vue项目webpack中Npm传递参数配置不同域名接口

    项目开发中,前端在配置后端api域名时很困扰,常常出现:本地开发环境: api-dev.demo.com测试环境: api-test.demo.com线上生产环境: api.demo.com, 这次是 ...

  4. vue2 如何通过router传递参数

    当需要router-link传递参数的时候 vue2 如何做 记录下来备忘 1.通过vue页面传递参数 <router-link :to="{ path:'./attachment', ...

  5. Vue 给子组件传递参数

    Vue 给子组件传递参数 首先看个例子吧 原文 html <div class="container" id="app"> <div clas ...

  6. [转] C++的引用传递、指针传递参数在java中的相应处理方法

    原文出处:[http://blog.csdn.net/conowen/article/details/7420533] 首先要明白一点,java是没有指针这个概念的. 但是要实现C++的引用传递.指针 ...

  7. 记一次WinForm程序中主进程打开子进程并传递参数的操作过程(进程间传递参数)

    目标:想在WinForm程序之间传递参数.以便子进程作出相应的处理. 一种错误的方法 父进程的主程序: ProcessStartInfo psi = new ProcessStartInfo(); p ...

  8. 在 Angularjs 中 ui-sref 和 $state.go 如何传递参数

    1 ui-sref.$state.go 的区别 ui-sref 一般使用在 <a>...</a>: <a ui-sref="message-list" ...

  9. Linux线程体传递参数的方法详解

    传递参数的两种方法 线程函数只有一个参数的情况:直接定义一个变量通过应用传给线程函数. 例子 #include #include using namespace std; pthread_t thre ...

  10. 【hadoop】如何向map和reduce脚本传递参数,加载文件和目录

    本文主要讲解三个问题:       1 使用Java编写MapReduce程序时,如何向map.reduce函数传递参数.       2 使用Streaming编写MapReduce程序(C/C++ ...

随机推荐

  1. tar解压报错——Not found in archive tar: Exiting with failure status due to previous errors

    tar解压报错--Not found in archive [root@master software]# tar -xzf scala-2.11.8.tgz /usr/local/ tar: /us ...

  2. 解决 idea web项目没有小蓝点的问题

    在idea导入web项目,项目没有显示小蓝点,无法添加 java文件和运行.如下图的springboot-schedule 和 springboot-test 都没有蓝点: 解决方案一: 点击 Fil ...

  3. idea 使用 mvn clean package 报错 Could not create local repository at

    使用 mac 版本的 idea 打包使用打包命令 mvn clean package 总是报错: [ERROR] Could not create local repository at /Repos ...

  4. css做多列瀑布流

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  5. 面向对象编程 es5和es6的构造函数

     /*         面向对象编程                          本质 创建一个对象                  可以用 属性属性值的 方式 存储 数据参数         ...

  6. 喜讯!极限科技再次中标中国移动云 Elasticsearch 自研版技术开发服务项目!

    喜讯!极限科技 再次中标 中国移动云 Elasticsearch 自研版技术开发服务项目! 近日,极限科技再次成功中标中国移动苏州研发中心 <云能力中心 2023-2024 年移动云 Elast ...

  7. 刀剑英雄 刀剑Online 双开 多开 窗口在后台 画面不动

    刀剑英雄 刀剑Online 双开 多开 窗口在后台 画面不动 解决方法: 进游戏前,在游戏设置中,选择"后台渲染",然后再进游戏.

  8. Kubernetes监控手册05-监控Kubelet

    上一篇我们介绍了如何监控Kube-Proxy,Kube-Proxy的/metrics接口没有认证,相对比较容易,这一篇我们介绍一下Kubelet,Kubelet的监控相比Kube-Proxy增加了认证 ...

  9. 计算机网络实验一:vlan的创建与划分

    这个是 pkt文件 有两道题 所以我是两个文件 https://pan.quark.cn/s/d4170897cb59 https://pan.quark.cn/s/da48878c77f5 发现 复 ...

  10. 支付宝签名和验签使用JSONObject是最优解。json字符串顺序和==符号都一致演示代码

    支付宝签名和验签使用JSONObject是最优解.json字符串顺序和==符号都一致演示代码 支付宝spi接口设计验签和返回结果加签注意点,支付宝使用JSONObject对象https://www.c ...