How to pass an array as a URL parameter?

前端 未结 5 1721
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 07:04

I would like to pass an array and added to a link on my page as a URL parameter, because later on the server side I need the values from the array. How should I do that?

5条回答
  •  有刺的猬
    2021-01-17 07:48

    I'd go with this approach,

    var myArray = ['aaa', 'bbb', 'ccc', ];
    
    var myArrayQry = myArray.map(function(el, idx) {
        return 'myArray[' + idx + ']=' + el;
    }).join('&');
    
    // myArray[0]=aaa&myArray[1]=bbb&myArray[2]=ccc
    

    Then, I retrieve the URL query params as an array on the server side.

提交回复
热议问题