4

I've got some jQuery (1.6.2, latest stable) code which is working in Chrome, Firefox, Safari, IE7 and IE8... but IE9 is failing to parse my json ajax response. The json is valid and I've run it through http://jsonlint.com/

$.ajax({
    url: lookupURL,
    dataType: "json",
    cache: false, // don't cache the result
    contentType: "application/json", //tell the server we're looking for json
    success: function(data) {
        // do stuff with result
    },
    error: function(xhr, errorString, exception) {
        alert("xhr.status="+xhr.status+" error="+errorString+" exception=|"+exception+"|");
    }
});

The error handler is the one jQuery calls (IE9 only). The xhr.status=200, the errorString=parseerror and the exception=SyntaxError JSON.parse

My json IS valid and I've even checked with using an ultimately simple json string:

{"foo":"bar"}

I have verified using xhr.responseText that there are no leading or trailing spaces on the json.

Why is this failing in IE9?

5
  • I would try removing the contentType option first since IE doesn't like <script type="application/javascript", past that, i dont know what would cause it. like you said, your json is valid. Commented Aug 24, 2011 at 22:02
  • I didn't have the contentType there to begin with, I added it while trying to solve this because I found that suggestion on someone's blog. Removing contentType doesn't solve the problem. :( Commented Aug 24, 2011 at 22:05
  • Is this on localhost? Then try this: Page-> Settings-> Compatibility mode-> Show intranet sites in compatibilty mode Commented Aug 24, 2011 at 23:01
  • Could it be this? social.msdn.microsoft.com/Forums/en/iewebdevelopment/thread/… Commented Aug 25, 2011 at 16:59
  • This isn't running on an intranet site, so it shouldn't matter, but just in case... yes, I have tried this and it doesn't change anything. Commented Aug 25, 2011 at 19:02

2 Answers 2

2

Found the problem. The system I'm working with is a fairly large CMS and E-Commerce framework, so they have a LOT of javascript in their own libraries. Deep inside one of their js libraries they were replacing the global JSON object and providing their own implementation of JSON.parse. It looks like it was an older and/or hacked version of json2 from json.org. When trying to solve the problem earlier, I had tried installing json2 as the JSON object to no avail... but it turned out they were then clobbering my json2 with theirs. I moved my installation of json2 to be the last javascript loaded and now it is working. I don't know what IE9 was the only one affected... but there you go.

Sign up to request clarification or add additional context in comments.

Comments

1

A couple of things for you to try, but first remove the contentType for now as I don't think you need it to work out your bug.

1) From here: http://api.jquery.com/jQuery.ajax/ dataType "As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require." So you could try your dataType as 'text json'

2) Is your json trimmed (no whitespace around it) ?

3) Have you tried it (at least as a test) using getJSON() ?

1 Comment

I tried "text json" as my dataType already. No help. My json is trimmed, I verified that. I haven't tried getJSON(), I'll do that now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.