Created
February 3, 2014 02:52
-
-
Save kawanet/8778224 to your computer and use it in GitHub Desktop.
This encodes a query parameter object as `key=value` pairs of `application/x-www-form-urlencoded`. Uses JSON when an object given as a value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {Object} query - query parameter object | |
* @param {String} `application/x-www-form-urlencoded` encoded string | |
*/ | |
function param(query) { | |
var list = []; | |
for(var key in query) { | |
var val = query[key]; | |
if ('object' === typeof val) { | |
val = JSON.stringify(val); | |
} | |
key = encodeURIComponent(key); | |
val = encodeURIComponent(val); | |
list.push(key + "=" + val); | |
} | |
return list.join("&"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
何度も書いたシリーズ。Object から a=b&c=d 形式の文字列に変換する。
値がオブジェクトの場合は、JSON 化する。