Is there any native function to convert json to url parameters?

后端 未结 11 1909
再見小時候
再見小時候 2020-11-29 22:00

I need convert json object to url form like: \"parameter=12&asd=1\"

I done with this:

        var data = {
            \'action\':\'actualiza_res         


        
11条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 22:51

    This one processes arrays with by changing the nameinto mutiple name[]

    function getAsUriParameters (data) {
      return Object.keys(data).map(function (k) {
        if (_.isArray(data[k])) {
          var keyE = encodeURIComponent(k + '[]');
          return data[k].map(function (subData) {
            return keyE + '=' + encodeURIComponent(subData);
          }).join('&');
        } else {
          return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]);
        }
      }).join('&');
    };
    

提交回复
热议问题