I been working with some java script, JQuery to be exact and I am having some problems with my Ajax call in IE9, everything works fine in Firefox4 and I get no error however in IE9 console when I am running the script I am getting following error
SCRIPT5009: 'JSON' is undefined
FeedbackComment.js, line 49 character 17
The code is very simple and standard
$.ajax({
type: "POST",
url: defaults.WebServiceURL,
data: "{ 'collectedFeedback':" + JSON.stringify(collectedFeedback) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
}
});
There also webservice behind scenes and class that represents collected feedback. However the problem seems to be not related to them.
I am not sure what is happening, can anyone help me please. Thanks in advance
I been working with some java script, JQuery to be exact and I am having some problems with my Ajax call in IE9, everything works fine in Firefox4 and I get no error however in IE9 console when I am running the script I am getting following error
SCRIPT5009: 'JSON' is undefined
FeedbackComment.js, line 49 character 17
The code is very simple and standard
$.ajax({
type: "POST",
url: defaults.WebServiceURL,
data: "{ 'collectedFeedback':" + JSON.stringify(collectedFeedback) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
}
});
There also webservice behind scenes and class that represents collected feedback. However the problem seems to be not related to them.
I am not sure what is happening, can anyone help me please. Thanks in advance
Share Improve this question edited Jun 7, 2011 at 14:03 Dmitri S. asked Jun 7, 2011 at 14:01 Dmitri S.Dmitri S. 3,4485 gold badges37 silver badges41 bronze badges 3-
1
You're trying to reference a variable called JSON -
JSON.stringify(collectedFeedback)
- that hasn't been defined - likely because the code that defines it doesn't work in IE9. Can you post that code? – Anthony Grist Commented Jun 7, 2011 at 14:06 - 1 Looks like the JSON library is not being loaded by IE? assuming JSON.stringify is on line 49 – WraithNath Commented Jun 7, 2011 at 14:07
- 1 You guys absolutely right! I added json2 from github./douglascrockford/JSON-js/blob/master/json2.js and things started to work again. Thanks a lot I appreciate your help! – Dmitri S. Commented Jun 7, 2011 at 14:19
3 Answers
Reset to default 4Just want to add that if you are in IE and it defaults into quirks mode or IE 7 or earlier patibility mode JSON will not be available.
Any time I ever have issues with IE 7,8 or 9, the first thing I check is the charset and ensure it's forced to utf-8. It's very picky about this.
Another thing to keep an eye is content-type header. Ensure it is correct and matches up with the response your sending back. It also tends to be picky about this.
So, for example, if you're making an ajax request and expecting a json response, you should set your content-type to 'application/json; charset=utf-8;'. If you're making an ajax request and expecting html as your response, your content-type should be set to 'text/html; charset=utf-8;'.
Hope this helps someone, as it has me.
I had this problem. Code was working in some pages, and not others. Thanks to the ment from "Rocket Hazmat" above, I was able to identify that the page just needed:
<!DOCTYPE html>
at the top. Without that, the JSON object does not exist! And IE behaves like something out of ancient history. No wonder Microsoft is dumping this code base for a fresh start on a new browser.