Query-string encoding of a Javascript Object

前端 未结 30 2947
渐次进展
渐次进展 2020-11-22 00:23

Do you know a fast and simple way to encode a Javascript Object into a string that I can pass via a GET Request?

No jQuery, no

30条回答
  •  时光说笑
    2020-11-22 01:06

    Here's the coffeescript version of accepted answer. This might save time to someone.

    serialize = (obj, prefix) ->
      str = []
      for p, v of obj
        k = if prefix then prefix + "[" + p + "]" else p
        if typeof v == "object"
          str.push(serialize(v, k))
        else
          str.push(encodeURIComponent(k) + "=" + encodeURIComponent(v))
    
      str.join("&")
    

提交回复
热议问题