简体   繁体   English

Axios参数中的多个值(逗号分隔)

[英]Axios multiple values (comma separated) in a parameter

Expected query string: 预期查询字符串:

http://fqdn/page?categoryID=1&categoryID=2

Axios get request: Axios收到请求:

fetchNumbers () {
  return axios.get(globalConfig.CATS_URL, {
    params: {
      ...(this.category ? { categoryId: this.category } : {})
    }
  })
    .then((resp) => {
      // console.log(resp)
    })
    .catch((err) => {
      console.log(err)
    })
}

As you can see, it works perfectly with just 1 value for 1 parameter, but if i wanted to make multiple values - it doesn't work, i've tried to use an array: 如您所见,它仅对1个参数的1个值有效,但是如果我想创建多个值-它不起作用,我尝试使用数组:

...(this.category ? { categoryId: [1, 2] } : {})

But it returns this way: 但是它以这种方式返回:

http://fqdn/page?categoryID[]=1&categoryID[]=2

So it just not working. 因此,它只是不起作用。 Had a look at this issue: Passing an object with a parameter with multiple values as a query string in a GET using axios 看了这个问题: 使用axios在GET中将带有多个值的参数的对象作为查询字符串传递

But can't figure out, how he solved this problem. 但不知道他如何解决这个问题。

This is not an axios related issue. 这不是与axios相关的问题。 It depends on whether your backend service is able to understand query params in this fashion(seems to be framework dependent). 这取决于您的后端服务是否能够以这种方式理解查询参数(似乎取决于框架)。 From your question, I think it is not working when queryParams like following are sent 从您的问题来看,当发送如下的queryParams时,我认为它不起作用

?categoryID[]=1&categoryID[]=2

and it expects 它期望

?categoryID = 1,2

What you can do is transform array to such string before passing it to params in axios. 您可以做的是在将数组传递给axios中的params之前将其转换为这样的字符串。 Update the following piece in your code and it should solve your problem. 更新代码中的以下内容,它应该可以解决您的问题。

...(this.category ? { categoryId: this.category.join(',') } : {})

Take a look at following thread 看一下下面的线程

How to pass an array within a query string? 如何在查询字符串中传递数组?

You can use Axios's paramsSerializer to customize the serialization of parameters in the request. 您可以使用Axios的paramsSerializer定制请求中参数的序列化。

Note that URLSearchParams serializes array data the way you're expecting: 请注意, URLSearchParams以您期望的方式序列化数组数据:

const searchParams = new URLSearchParams();
searchParams.append('foo', 1);
searchParams.append('foo', 2);
console.log(searchParams.toString()); // foo=1&foo=2

So you could use that class in paramsSerializer as follows: 因此,您可以在paramsSerializer使用该类,如下所示:

// my-axios.js
export default axios.create({
  paramsSerializer(params) {
    const searchParams = new URLSearchParams();
    for (const key of Object.keys(params)) {
      const param = params[key];
      if (Array.isArray(param)) {
        for (const p of param) {
          searchParams.append(key, p);
        }
      } else {
        searchParams.append(key, param);
      }
    }
    return searchParams.toString();
  }
});

// Foo.vue
import axios from './my-axios.js';

export default {
  methods: {
    async send() {
      const { data } = await axios({
        url: '//httpbin.org/get',
        params: {
          categoryId: [1, 2, 3]
        }
      });

      // ...
    }
  }
}

demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM