Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this jQuery Ajax call fail ONLY in IE9 (Even works fine in IE8 and IE7)

I have a website where I make an ajax call like this:

            // perform an ajax request to generate a new subscriber account
            jQuery.ajax({
                type: 'POST',
                url: '/index.php?option=com_content&view=article&id=45&tmpl=component',
                data: postVars,
                success: handleResponse,
                error: function(jqXHR, textStatus, errorThrown) {
                    alert ('response: ' + jqXHR.responseText);
                    alert ('code: ' + jqXHR.getResponseHeader('X-Subscriber-Status'));
                }
            });

The page being requested doesn't do anything but return a single digit status code and it works fine with every browser except for IE9. The script is hosted on the same domain as the page it's requesting so there shouldn't be any issue with cross domain scripting.

I finally tracked down what's happening but I don't know how to fix it. It turns out that the Ajax request completes successfully with no problems. I verified this by using Fiddler to look a the network traffic. The server responds to the request with an HTTP status code of 200 and the body doesn't contain anything but a single digit response code. At this point jQuery executes the error handler, NOT the success handler as would be expected (in IE9). Why does it do this and how can I prevent it?? This ONLY happens in IE9. Even IE8 and IE7 work just fine with the exact same code! As you can see I finally resorted to detecting IE9 and using an XmlHttpRequest object to do it (which works just fine by the way).

This seems like a jQuery bug to me but I can't find any mention of it in my searches. Am I really the only one experiencing this odd behavior?

like image 888
rushinge Avatar asked Aug 02 '11 00:08

rushinge


1 Answers

Well I finally tracked down the problem. It turns out that for some reason jQuery/IE does not correctly urlencode double quotes. The URL of the request was:

/search.json?callback=jQuery16105234487927080267_1313510362362&q=stocks OR "stock market" -blueprint -empireavenue.com -learn&_=1313510362469

In every other browser by the time jQuery performed the ajax request it looked like:

/search.json?callback=jQuery16105234487927080267_1313510362362&q=stocks%20OR%20%22stock%20market%22%20-blueprint%20-empireavenue.com%20-learn&_=1313510362469

but for whatever reason in all versions of IE it came out like this:

/search.json?callback=jQuery16105234487927080267_1313510362362&q=stocks%20OR%20"stock%20market"%20-blueprint%20-empireavenue.com%20-learn&_=1313510362469

which caused the server to return no data.

like image 56
rushinge Avatar answered Dec 26 '22 18:12

rushinge