如何使用 axios 序列化参数 [英] how to serialize parameters using axios

查看:50
本文介绍了如何使用 axios 序列化参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何序列化 url 参数以用于 axios?我的参数只是数组 [1,2] 中的数字.到目前为止,这是我的代码

How would I serialize url parameters for use with axios? My parameters are just numbers in array [1,2]. Here is my code so far

     axios({
       method: 'delete',
       url: '/api/'
     }) 

我的请求 url 将类似于 http://127.0.0.1:8000/api/?id=1&id=2

My request url will be something like this http://127.0.0.1:8000/api/?id=1&id=2

我查看了 axios 具有的 paramsSerializer 方法,但它令人困惑的是如何使用它,或者它是否适合我的情况.请指教.谢谢

I looked at the paramsSerializermethod that axios has but its confusing how it can be used or whether its even appropriate in my case. Please advice. Thanks

推荐答案

axios.getconfig 对象接受 params.在 params 中,您可以指定您的数组,它会为您进行转换.

the config object of axios.get accepts params. In params you can specify your array and it will do the conversion for you.

这是一个例子:

axios.get('/api/', {
  params: {
   id: [1,2]
  }
}) 

这将发出如下所示的请求:

This will make a request which looks like this:

/api/?id[]=1&id[]=2

这篇关于如何使用 axios 序列化参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆