How to pass an array within a query string?

前端 未结 10 1353
一整个雨季
一整个雨季 2020-11-22 02:35

Is there a standard way of passing an array through a query string?

To be clear, I have a query string with multiple values, one of which would be an array value.

10条回答
  •  青春惊慌失措
    2020-11-22 03:06

    Although there isn't a standard on the URL part, there is one standard for JavaScript. If you pass objects containing arrays to URLSearchParams, and call toString() on it, it will transform it into a comma separated list of items:

    let data = {
      str: 'abc',
      arr: ['abc', 123]
    }
    
    new URLSearchParams(data).toString();  // ?str=abc&arr=abc,123
    

提交回复
热议问题