Check out "Do you speak JavaScript?" - my latest video course on advanced JavaScript.
Language APIs, Popular Concepts, Design Patterns, Advanced Techniques In the Browser

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.