This question already has an answer here:
- Query-string encoding of a Javascript Object 31 answers
I'm trying to find information on how to serialize an object to query string format, but all my searches are drowning in results on how to go the other way (string/form/whatever to JSON).
I have
{ one: 'first', two: 'second' }
and I want
?one=first&two=second
Is there a good way to do this? I don't mind plugins or whatnots - if the code I find is not a plugin, I'll probably re-write it to one anyway...
Another option might be node-querystring.
It's available in both
npm
andbower
, which is why I have been using it.For a quick non-JQuery function...
Note this doesn't handle arrays or nested objects.
You want
$.param()
: http://api.jquery.com/jQuery.param/Specifically, you want this:
When given something like this:
$.param
will return this:Alternatively YUI has http://yuilibrary.com/yui/docs/api/classes/QueryString.html#method_stringify.
For example: