cnu cnu - 10 months ago 50
Javascript Question

How to create query parameters in Javascript?

Is there any way to create the query parameters for doing a GET request in Javascript?

Just like in Python you have urllib.urlencode(), which takes in a dict (or list of two tuples) and creates a string like


Answer Source

Here you go:

function encodeQueryData(data) {
   let ret = [];
   for (let d in data)
     ret.push(encodeURIComponent(d) + '=' + encodeURIComponent(data[d]));
   return ret.join('&');


var data = { 'first name': 'George', 'last name': 'Jetson', 'age': 110 };
var querystring = encodeQueryData(data);