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 other frameworks - just plain Javascript :)
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 other frameworks - just plain Javascript :)
Can you use
URLSearchParams
?just
new URLSearchParams(object).toString()
Chrome 49+, though
URLSearchParams - Web API
Can I use URLSearchParams
A small amendment to the accepted solution by user187291:
Checking for hasOwnProperty on the object makes JSLint/JSHint happy, and it prevents accidentally serializing methods of the object or other stuff if the object is anything but a simple dictionary. See the paragraph on for statements in this page: http://javascript.crockford.com/code.html
use JSON.
take a look at this question for ideas on how to implement.
I suggest using the URLSearchParams interface:
Here's a concise & recursive version with Object.entries. It handles arbitrarily nested arrays, but not nested objects. It also removes empty elements:
E.g.:
Refer from the answer @user187291, add "isArray" as parameter to make the json nested array to be converted.
To make the result :
staffId=00000001&Detail[0].identityId=123456&Detail[1].identityId=654321