views:

934

answers:

3

While trying to GET a JSON, my callback function is NOT firing.

$.ajax({ type:"GET",
         dataType:'json',
         url: myLocalURL,
         data: myData,
         success:
           function(returned_data){alert('success');}
       });

The strangest part of this is that

  1. my JSON(s) validates on JSONlint
  2. this ONLY fails on IE7...it works in Safari, Chrome, and all versions of Firefox, (EDIT: and even in IE8). If I use 'error', then it reports "parseError"...even though it validates!

Is there anything that I'm missing? Does IE7 not process certain characters, data structures (my data doesn't have anything non-alphanumeric, but it DOES have nested JSONs)? I have used tons of other AJAX calls that all work (even in IE7), but with the exception of THIS call.

An example data return (EDIT: This is a structurally-complete example, meaning it is only missing a few second-tier fields, but follows this exact hierarchy)here is:

{"question":{
             "question_id":"19",
             "question_text":"testing",
             "other_crap":"none"
            },
  "timestamp":{
              "response":"answer",
              "response_text":"the text here"
              }
}

I am completely at a loss. Hopefully someone has some insight into what's going on...thank you!

EDIT Here's a copy of the SIMPLEST case of dummy data that I'm using...it still doesn't work in IE7.

{
  "question":{
         "question_id":"20",
         "question_text":"testing :",
         "adverse_party":"none",
         "juris":"California",
         "recipients":"Carl Chan"
         }
}

EDIT 2 I am starting to doubt that it is a JSON issue...but I have NO idea what else it could be. Here are some other resources that I've found that could be the cause, but they don't seem to work either: http://firelitdesign.blogspot.com/2009/07/jquerys-getjson.html (Django uses Unicode by default, so I don't think this is causing it)

Anybody have any other ideas?

ANSWER

I finally managed to figure it out...mostly via tedious trial-and-error. I want to thank everyone for their suggestions...as soon as I have 15 rep, I'll upvote you, I promise. :)

There was basically no way that you guys could have figured it out, because the issue turned out to be a strange bug between IE7 and Django (my research didn't bring up any similar issues). We were basically using Django template language to generate our JSON...and in the midst of this particular JSON, we were using custom template tags:

{% load customfilter %}
{ "question":{ "question_id":"{{question.id}}",
               "question_text":"{{question.question_text|customfilterhere}}"
             }
}

As soon as I deleted anything related to the customfilter, IE7 was able to parse the JSON perfectly! We still don't have a workaround yet, but at least we now know what's causing it.

Has anyone seen any similar issues? Once again, thank you everyone for your contributions.

+2  A: 

The example data you present looks all right but my strong suspicion still is that there is an unclosed comma somewhere like this:

 "timestamp":{
              "response":"answer",
              "response_text":"the text here"
              }, <------------
}

IE is the only browser that (correctly) trips over this.

If this is not it, can you show a full data sample (or confirm that the example you show is indeed a full sample)?

Pekka
thanks for the suggestion!I can confirm that this is a full example (minus some second-tier fields). Let me try your suggestion by adding commas even to the last key-values of each JSON.
Kenny Leu
D'oh. Nope, that didn't work.
Kenny Leu
No, what Pekka is saying is that you CANNOT have a comma on the last element. IE will reject it.
BRH
haha oh i see...nope there definitely aren't any lingering commas at the ends.
Kenny Leu
@Kenny very strange. Is there any way to get more detailed error info from the JSON interpreter? I'm afraid I'm not familiar with it. Any special characters in the input - quotes, non-english characters?
Pekka
@Pekka I sure wish I could. The problem is that this is IE7...so Firebug won't work. It also WORKS in IE8 so I can't use that IE8's debugger either.I'm attaching a real copy of the dummy data that is getting passed in the original post.
Kenny Leu
@Kenny How about IE8 in IE7 mode?
Pekka
You sir, are a genius!Give me some hours to pick through it. And to confirm, it doesn't work in IE7 mode on IE8, so I'll be able use the debugger.
Kenny Leu
@Kenny great stuff. Good luck!
Pekka
fyi, here's the validator that I'm using:http://www.jsonlint.com/very neat tool, but in this case it is not detecting what's wrong. I'm starting to suspect that it's not a JSON issue, but I have NO idea what else it could be.
Kenny Leu
+1  A: 

Did you already exclude the possibility of a caching issue?

e.g. you tested with IE7 when myLocalURL returned invalid json. IE7 still caches that response and thus it doesn't work. Try adding something like this (e.g. if php) to myLocalURL or make myLocalURL look like myLocalURL?random=123 just for testing to make sure it isn't a caching thing

header("Cache-Control: no-cache, must-revalidate");
header("Expires: 0");

Are you returning a correct content-typ header? e.g.

header("Content-Type: application/json");
jitter
Although we're using Python, thanks for the suggestion!I thought that it may be a caching issue as well...so I turned on the developer toolbar and turned on 'Always refresh from server'. Not to mention that I've tried it on several different computers, all with the same results. So it couldn't be client-side caching.I'm using $.ajax which allows contentType: "application/json", so that base is covered as well.
Kenny Leu
A: 

I've just encountered exactly the same issue. It turns out that IE7 fails to parse JSON responses that have leading \r\n line feeds in the response body. Your fix of removing {% load customfilter %} works because you removed the new line that was being included after this tag.

An alternative fix would be to just remove the new line to get

{% load customfilter %}{ "question":{ "question_id":"{{question.id}}",
           "question_text":"{{question.question_text|customfilterhere}}"
         }
}
Snukker
Thank you for your answer!! Although it is too late for me to test, I would NOT be surprised if what you say is true. <fist shake>IE7!!!!!!!</fist shake>
Kenny Leu