Skip to content

Instantly share code, notes, and snippets.

@kudinovfedor
Created April 10, 2019 07:21
Show Gist options
  • Save kudinovfedor/9a1878c4a150dd40187edc069eaef5b5 to your computer and use it in GitHub Desktop.
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')
/**
* 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