[RESOLVED]IE8 and IE9 classifying JSON response-object as undefined

I wrote some code to manually invoke ajax instead of using jquery. It works fine in Firefox and IE10, but not in IE8 and 9.  The ‘POST’ works fine, actually, but the response-object arrives as ‘undefined’ in IE8 and IE9 (I’m using the ‘Mult-Browser’ simulator
program to run IE8 and 9).   I stepped through the code on the .Net side, in debug mode, to verify that the return Json(collection) is in fact a populated collection (has 34 items).

This is NOT a JSON.parse issue.  The issue is that JSON.parse can’t parse an undefined object. JSON.parse is working fine in IE10 and Firefox because the debugger isn’t showing ‘undefined’ for these browers.

Why  isn’t the .Net code ‘return json(collection)’ working for IE8 and 9?

Here’s my ajax code:

        function ajax(data, url, requestType, IsJSON, funcOnFailure, funcOnSuccess){
            var http = new XMLHttpRequest();
            http.open(requestType, url, true);
            if (requestType.toUpperCase() == "POST") {
                var postThisString = encodeDataAsPostString(data);
                http.setRequestHeader("Content-length", postThisString.length);
                if (IsJSON) {
                    http.setRequestHeader("Content-type", "application/json; charset=utf-8");
                }
                else http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            }
            http.setRequestHeader("Connection", "close");
            http.onreadystatechange = function () {//Call a function when the state changes.
                if (http.readyState != 4) return;
                if (http.status == 200) {
                    if (funcOnSuccess) funcOnSuccess(http.response);
                }
                else {
                   if (funcOnFailure) funcOnFailure("An error occurred: " + http.statusText);
                }
            }
            http.send(postThisString);
        }

The code above was  capturing httpReq.response  – which worked fine in Firefox and IE10 with JSON.parse. What seems to work for these browsers plus for IE8 as well is to  capture httpReq.responseText.

Until a moment ago I wasn’t even aware of this distinction.

Leave a Reply