0

I'm using jQuery 1.8 and I have an ajax call that returns JSON. If there's an error, it only returns { "status": "there was an error" } otherwise, it returns a document which is the data that the ajax request means to load which will look something like { "document": { ... } }

On Firefox and Chrome the following code works, but on IE8 I'm getting an error saying data.status is null or not an object (when the URL requested clearly does return a document and not just a status) which then causes the script to crash. Does anybody know how to get around this error message on IE8?

$.ajax({
    url: "GanttLoader.ashx?action=loadGantt&gantt=" + current_selected_gantt + "&userId=" + userId,
    context: document.body,
    type: "GET",
    dataType: "json",
    success: function (data) {
        if (data.status != null) {
            if (data.status == "none") {
                alert("no gantts found when attempted to load");
            } else if (data.status == "locked") {
                alert("this gantt is locked");
            }
        } else if (data.document != null) {
            /* process the gantt */
        }
    },
    error: function () {
        alert("couldn't load gantt charts");
    }
});
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
David
  • 2,080
  • 5
  • 29
  • 44
  • When the URL returns a `document`, does it also contain a `status`? – gen_Eric Sep 10 '12 at 15:05
  • No, there isn't really a need to do so. If @Aitor-Calderon s idea doesn't work I'll try that. – David Sep 10 '12 at 15:14
  • On which line are you getting this error? – gen_Eric Sep 10 '12 at 15:16
  • The error message remains the same even though a status is always returned now. I'm certain the JSON is parsing correctly because FF and Chrome handle it just fine. Does anybody know of any other JSON problems using jQuery on IE8? – David Sep 10 '12 at 15:21
  • @Rocket, on the very first appearance of `data.status` in the javascript, the first if in the success function. – David Sep 10 '12 at 15:22
  • Try using `if(data.hasOwnProperty('status'))` instead of checking status against _null_. – Simon Sep 10 '12 at 15:43
  • @Simon, with that I get an error saying 'null' is null or not an object. Even though earlier I tried `alert(typeof data)` which printed 'object'. – David Sep 10 '12 at 16:03
  • This hardly seems like the object, if there is one in IE, is not what we expect it to be, what does it tell you when you `console.log(data)` in IE? – Simon Sep 10 '12 at 16:06
  • @Simon, the IE8 developer tools console only shows the same 'null' is null or not an object error message. Are there any other ways I can load some JSON returned from another .ashx file when a drop down is clicked? – David Sep 10 '12 at 16:14
  • I'm no .NET programmer but from what I've seen they always set the `contentType` setting of their ajax call to `'application/json; charset=utf-8'`, maybe you could try that. ([asp net and ajax](http://stackoverflow.com/questions/2948628/asp-net-passing-json-from-jquery-to-ashx)) – Simon Sep 11 '12 at 07:06
  • @Simon I set the dataType on the Ajax call to json and the .ashx handler I set the Content-Type as well as the Charset. However the problem was in the userId variable which was null causing the .ashx handler to break and return nothing. – David Sep 13 '12 at 19:54

3 Answers3

0

Make sure the Content-Type of your response is application/json

Martin Ernst
  • 5,629
  • 2
  • 17
  • 14
0

Could it be that instead of null, data.status is "undefined"? maybe testing data.status with undefined would do the job right.

if (data.status != undefined)
{
...
}
Aitor Calderon
  • 763
  • 6
  • 16
0

The JSON response was actually not sending nothing because in IE8 userId was null, breaking my .ashx handler. Because my cookie parsing code (not shown) worked on Chrome / FF but not on IE I thought it the url was being entered correctly. I moved to the jquery cookie plugin and the problem was resolved (why is cookie parsing and setting not standard in jQuery?). Thanks for all of your help even though it was a mistake on my part.

David
  • 2,080
  • 5
  • 29
  • 44