Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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 'var1=value1&var2=value2'.

share|improve this question
up vote 93 down vote accepted

Here you go:

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

Usage:

var data = { 'first name': 'George', 'last name': 'Jetson', 'age': 110 };
var querystring = encodeQueryData(data);
share|improve this answer
6  
When iterating with for, use hasOwnProperty to ensure interoperability. – troelskn Sep 21 '08 at 19:29
2  
@troelskn, good point... although in this case, someone would have to be extending Object.prototype to break it, which is a pretty bad idea to start with. – Shog9 Sep 21 '08 at 19:57
1  
@Shog9 Why is it a bad ideia? – Cesar Canassa Sep 14 '11 at 23:55
    
@Cesar, see: stackoverflow.com/questions/3832617/…** – Shog9 Sep 15 '11 at 0:01
4  
api.jquery.com/jQuery.param – Zabba Jan 11 '13 at 21:17

fun(ctional)

function encodeData(data) {
    return Object.keys(data).map(function(key) {
        return [key, data[key]].map(encodeURIComponent).join("=");
    }).join("&");
}   
share|improve this answer
1  
Nice one! .map() has been implemented in JavaScript 1.6 so almost all browsers, even the granny ones support it. But as you can guess IE does not except IE 9+. But do not worry, there is a workaround. Source: developer.mozilla.org/en-US/docs/JavaScript/Reference/… – Akseli Palén Feb 13 '13 at 11:24

Zabba has provided in a comment on the currently accepted answer a suggestion that to me is the best solution: use jQuery.param().

If I use jQuery.param() on the data in the original question, then the code is simply:

var params = jQuery.param({
    var1: 'value',
    var2: 'value'
});

The variable params will be

"var1=value&var2=value"

For more complicated examples, inputs and outputs, see the jQuery.param() documentation.

share|improve this answer

If you are using Prototype there is Form.serialize

If you are using jQuery there is Ajax/serialize

I do not know of any independent functions to accomplish this, though, but a google search for it turned up some promising options if you aren't currently using a library. If you're not, though, you really should because they are heaven.

share|improve this answer
5  
jQuery.param() takes object and transform it to GET query string (this function better matches the question). – azurkin Jul 21 '15 at 18:20

We've just released arg.js, a project aimed at solving this problem once and for all. It's traditionally been so difficult but now you can do:

var querystring = Arg.url({name: "Mat", state: "CO"});

And reading works:

var name = Arg("name");

or getting the whole lot:

var params = Arg.all();

and if you care about the difference between ?query=true and #hash=true then you can use the Arg.query() and Arg.hash() methods.

share|improve this answer

A little modification to typescript:

  public encodeData(data: any): string {
    return Object.keys(data).map((key) => {
      return [key, data[key]].map(encodeURIComponent).join("=");
    }).join("&");
  }
share|improve this answer

This thread points to some code for escaping URLs in php. There's escape() and unescape() which will do most of the work, but the you need add a couple extra things.

function urlencode(str) {
str = escape(str);
str = str.replace('+', '%2B');
str = str.replace('%20', '+');
str = str.replace('*', '%2A');
str = str.replace('/', '%2F');
str = str.replace('@', '%40');
return str;
}

function urldecode(str) {
str = str.replace('+', ' ');
str = unescape(str);
return str;
}
share|improve this answer
1  
encodeURIComponent handles this and doesn't incorrectly use + for a space. – AnthonyWJones Sep 21 '08 at 19:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.