My 5th book "50 Tips on JavaScript" is on sale. Go get it from
Illustrated guide to 50 of the most interesting JavaScript topics.

AJAX request doesn't work in IE (url encoding problem)

The good old Internet Explorer. It's full with surprises. I just fixed a bug happening only in IE. Everything works just great in every other browser.

The story

I have a simple mobile site, which makes Ajax requests to an external REST API. Some part of the application works, but others not. I noticed that the problematic requests contain JSON. I.e. something like:


In Google Chrome, the actual request string looks like that:


The backend understand the query, parses it and returns a proper result. However, without correct url encoding the things are not working.

The solution

During the url preparation I replaced

url += JSON.stringify(parameter);


url += encodeURIComponent(JSON.stringify(parameter));

And the problem is fixed. Now even the crappy IE works.

If you enjoy this post, share it on Twitter, Facebook or LinkedIn. To leave a comment go here.