How to pass an array as a URL parameter?

前端 未结 5 1740
隐瞒了意图╮
隐瞒了意图╮ 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 08:01

    You can serialize the JSON:

    myArray = ['aaa', 'bbb', 'ccc'];
    var arrStr = encodeURIComponent(JSON.stringify(myArray));
    $('#myLink').attr({ href: '/myLink?array=' + arrStr });
    

    If your parsing (on the next page) is done via JavaScript too, you'll conversely use JSON.parse(). In PHP it would be json_decode().

提交回复
热议问题