Created
April 10, 2019 07:21
-
-
Save kudinovfedor/9a1878c4a150dd40187edc069eaef5b5 to your computer and use it in GitHub Desktop.
Get string with URL parameters for using when Ajax post request ('Content-Type', 'application/x-www-form-urlencoded')
This file contains 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
/** | |
* Get URL Params | |
* | |
* @description String with URL parameters for using when Ajax post request | |
* | |
* @example | |
* getUrlParams({prop1: value1, prop2: value2}); | |
* | |
* @param {Object} object - object with properties and values | |
* @returns {string|null} | |
*/ | |
const getUrlParams = (object) => { | |
let key, value, array = [], params = null; | |
for (key in object) { | |
if (object.hasOwnProperty(key)) { | |
key = encodeURIComponent(key); | |
value = encodeURIComponent(object[key]); | |
array.push(`${key}=${value}`); | |
} | |
} | |
if (!array.length) return params; | |
params = array.join('&'); | |
return params; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment