How to pass an array as a URL parameter?

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

    Pass an array of strings as URL parameters:

    const myLink = 'https:/example.com/api'
    const myArray = ['aaa', 'bbb', 'ccc'];
    let apiUrl = `${myLink}/query?`;
    
    myArray.forEach((x) => {
        apiUrl += `&array=${x}`;
    });
    
    console.log(apiUrl);
    // https://example.com/api/query?array="aaa"&array="bbb"&array="ccc"
    

提交回复
热议问题