简体   繁体   中英

Axios get with param array

I create one get request with axios:

this.$axios.get('/cidade/',{
          params: {
            q: result,
          }
        })
        .then((response) => {
          this.cidades = response.data;
        })
        .catch(function (error) {
          // handle error
          // eslint-disable-next-line
          console.log(error);
        })
        .then(function () {

          // always executed
        });

but my result is an array [123asdas1,asdasd2312] and when the axios excute the request he create that url:

http://localhost:8081/cidade/?q[]=5b9cfd0170607a2e968b0a1e&q[]=5b9cfd0170607a2e968b0a1d

so It's possible to remove the [] from q param? how?

tks

When composing a query string where one field has multiple values (ie if it were an array), then there is no standard that says how it must be encoded in the query string, however most web servers accept this syntax:

http://localhost:8081/cidade/?q[]=value1&q[]=value2

which is why axios defaults to it. Check your web server to see if it is reading the parameter as an array correctly.

If you want to force it to be encoded in some other way, just convert the array to a string in whatever format you want and send it as a single value:

this.$axios.get('/cidade/', {
  params: {
    q: JSON.stringify(result)
  }
})
http://localhost:8081/cidade/?q=[value1,value2]

(The [ and ] characters may be percent-encoded .)

In general, this syntax cannot distinguish between the string "[value1,value2]" and the array [value1, value2] , so the web server will have to choose one or the other. Again, this is all dependent on your web server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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