How to escape a JSON string to have it in a URL?

前端 未结 6 506
迷失自我
迷失自我 2020-11-28 18:50

Using Javascript, I want to generate a link to a page. The parameters to the page are in a Javascript array that I serialize in JSON.

So I would like to generate a U

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 19:30

    Answer given by Delan is perfect. Just adding to it - incase someone wants to name the parameters or pass multiple JSON strings separately - the below code could help!

    JQuery

    var valuesToPass = new Array(encodeURIComponent(VALUE_1), encodeURIComponent(VALUE_2), encodeURIComponent(VALUE_3), encodeURIComponent(VALUE_4));
    
    data = {elements:JSON.stringify(valuesToPass)}
    

    PHP

    json_decode(urldecode($_POST('elements')));
    

    Hope this helps!

提交回复
热议问题