Home About Me

The IE Ajax Failure That Had Nothing to Do With jQuery

A friend of mine runs a website. He used jQuery's Ajax methods to fetch a chunk of HTML from the backend and insert it into a div on the page. Nothing unusual there—jQuery's load method is built for exactly this. His code looked like this:

var tab = jQuery("#dynamic_tab");
var url = "/list_ajax/";
tab.load(url);

It could hardly be simpler. It worked in Chrome, Firefox, and Safari. It failed only in IE—IE7, IE8, and IE9 alike. The symptom was strange: opening the Ajax URL directly in IE worked fine, but the respons object returned by jQuery's Ajax method was undefined. No error was reported.

He could not get it to work, so he Googled jQuery load IE. Many people had asked the same question. That started a round of shotgun programming.

The first hit was a StackOverflow question with about 33K views: jQuery’s .load() not working in IE – but fine in Firefox, Chrome and Safari. The answer had not been accepted, which was not reassuring. There were more similar StackOverflow questions, but no answers. I tried the suggested fixes anyway—shotgun style. Nothing worked.

Then I found people talking about IE caching: set cache to false, or use something like this:

var tab = jQuery("#dynamic_tab");
var fuckie = Math.random();
var url = "/list_ajax/"+"?fuckie="+fuckie;
tab.load(url);

Same result. I tried nearly everything and none of it worked. After a day of this, my friend said in frustration that Microsoft should hurry up and go out of business. IE really was awful.

I opened IE9's developer tools. I could see that when the Ajax link was clicked, IE made an HTTP Ajax request to the site and received the response, but the response never appeared on the page—jQuery's Ajax responseText was undefined.

I am an old-timer and not very familiar with jQuery, so I started debugging jQuery's code to see what it was doing and whether it was throwing an error. After an hour, I had a rough understanding of jQuery's Ajax wrapper, but I still had not found the cause.

So I wrote raw Ajax to see what IE's ActiveX Ajax object was actually doing. I had written Ajax this way years before, so it was not much trouble—and there were examples online to copy anyway:

function InitAjax()
{
var ajax=false;
try {
ajax = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
ajax = false;
}
}
if (!ajax && typeof XMLHttpRequest!='undefined') {
ajax = new XMLHttpRequest();
}
return ajax;
}

var ajax = InitAjax();
ajax.open("GET", url, true);
ajax.onreadystatechange = function() {
if (ajax.readyState == 4 && ajax.status == 200) {
var show = document.getElementById("HaoChenDIV").value;
show.innerHTML = ajax.responseText;
}
}
ajax.send(null);

It still did not work. IE did not report an error, but at least I was now sure this was not jQuery's fault. It was probably our own program. The program was much easier to debug at this level. In IE9, inside the native Ajax object's onreadystatechange function, responseText looked like this:

IE Ajax responseText showing the system error -1072896658

What is 系统错误: -1072896658? A Google search turned up a pile of pages. The basic explanation was garbled encoding: the Ajax backend was returning a page whose encoding IE did not recognize. The fix was to add charset=utf-8 to the returned HTTP header.

I changed the backend Ajax program to explicitly specify the charset in the HTTP header. IE worked normally. I switched back to the jQuery load code and everything worked. The backend had been using UTF-8 all along, but it did not explicitly specify that in the HTTP header. Only IE failed to detect it automatically.

The root cause was that we had not written the page according to the spec. That led me to think about other HTML rules, but there are too many to remember. Still, you may know that http://validator.w3.org can check many non-standard things in your pages. The tool reports a lot of errors, and many are nitpicky, but it is worth a look. (Today this site has many plugins and I have adjusted some things, so it reports many errors. I remember that when there were no plugins and no customized styles, WordPress reported not a single error.)

Afterword

I wanted to share this problem and the process for a few reasons, and I have a few questions worth thinking about:

1) Many people online have reported this problem, but answers are basically nowhere—including on StackOverflow. So I am writing it down to fill that gap.

2) I believe programmers run into this kind of thing all the time. I do not know what people usually do. Perhaps most search online for various solutions and try them one by one until something works—shotgun programming. Of course, most answers can probably be found. But after we find an answer, do we still dig into the specific cause and think about the surrounding issues?

3) In an era with so many frameworks, libraries, and open-source projects, have we lost the ability to write code from scratch? For example, with the problem above, how many people can still write raw Ajax? I still suggest that while using frameworks, people understand the most basic knowledge. Seeking a real understanding—knowing not just what works but why—really matters.

I am a programmer from the 'eating bran' era. Back then programmers had to do everything themselves, and it was hard. When I tell people today about those experiences, they laugh. But going through an era when I had to do everything myself benefited me a lot. I share these thoughts in the hope that they are useful.