40

I'm making an AJAX call to a WCF service and when I pass in my data i use JSON.stringify()

The call returns and works fine in FF, & Chrome, but not IE8. I get an error: 'JSON' is undefined

suggestions?

p.s. I also want this to work in IE7

KevinDeus
  • 11,988
  • 20
  • 65
  • 97
  • I don't think IE8 supports the `stringify()` method natively (there are libraries that implement it, but you haven't specified whether you're using any of them) – Spudley Feb 23 '11 at 16:24
  • it does. Look under 'cross-domain security' here: http://www.microsoft.com/windows/internet-explorer/readiness/developers-new.aspx – KevinDeus Feb 23 '11 at 16:41
  • Refer to the following http://www.devcurry.com/2010/12/resolve-json-is-undefined-error-in.html – Sugar Bowl May 29 '14 at 20:09

6 Answers6

53

Use json2 for a consistent cross browser implementation.

https://github.com/douglascrockford/JSON-js

river
  • 1,508
  • 1
  • 12
  • 17
  • would I reference JSON the same way? Will it conflict with IE8's native implementation? – KevinDeus Feb 23 '11 at 16:43
  • 2
    Yes. No (it checks for an existing JSON object). If IE has an implementation then I don't know why you are getting JSON undefined error (is it called something else?). I don't know enough about IE to comment further. – river Feb 23 '11 at 17:11
  • 2
    IE8 is supposed to have a native implementation. IE7 doesn't. If you're using a js toolkit like jquery or dojo, chances are it has it's own implementation of this so you might as well use that and avoid having to add yet another component to your codebase. – CptAJ Aug 15 '12 at 13:25
  • Note that if a quirks mode page is opened IE and it contains an iframe, that iframe will also be quirks mode and will not have the json lib. So JSON could be lacking even more recent IE versions if you don't control the parent frame a user is browsing you with. – cmc Oct 28 '14 at 14:42
27

I had the issue with IE9. IE9 was rendering my page in "quirks" mode, the solution was simply to add <!DOCTYPE html>. This took me out of "quirks" mode which I'm sure fixed more than just this issue!

Joe Albowicz
  • 270
  • 3
  • 4
19

Update

Check the JSON3 library. It works like a charm.

Changes from JSON2

I hope this helps.


Hope this helps. I got this from a few online sources long back. don't have their links.
Sorry that i'm unable to cite references.

var JSON = JSON || {};
// implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function(obj) {
    var t = typeof (obj);
    if (t != "object" || obj === null) {
        // simple data type
        if (t == "string")
            obj = '"' + obj + '"';
        return String(obj);
    } else {
        // recurse array or object
        var n, v, json = [], arr = (obj && obj.constructor == Array);
        for (n in obj) {
            v = obj[n];
            t = typeof (v);
            if (t == "string")
                v = '"' + v + '"';
            else if (t == "object" && v !== null)
                v = JSON.stringify(v);
            json.push((arr ? "" : '"' + n + '":') + String(v));
        }
        return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
    }
};
// implement JSON.parse de-serialization
JSON.parse = JSON.parse || function() {
    var r = "(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b)", k = '(?:[^\\0-\\x08\\x0a-\\x1f"\\\\]|\\\\(?:["/\\\\bfnrt]|u[0-9A-Fa-f]{4}))';
    k = '(?:"' + k + '*")';
    var s = new RegExp(
            "(?:false|true|null|[\\{\\}\\[\\]]|" + r + "|" + k + ")", "g"), t = new RegExp(
            "\\\\(?:([^u])|u(.{4}))", "g"), u = {
        '"' : '"',
        "/" : "/",
        "\\" : "\\",
        b : "\u0008",
        f : "\u000c",
        n : "\n",
        r : "\r",
        t : "\t"
    };
    function v(h, j, e) {
        return j ? u[j] : String.fromCharCode(parseInt(e, 16));
    }
    var w = new String(""), x = Object.hasOwnProperty;
    return function(h, j) {
        h = h.match(s);
        var e, c = h[0], l = false;
        if ("{" === c)
            e = {};
        else if ("[" === c)
            e = [];
        else {
            e = [];
            l = true;
        }
        for ( var b, d = [ e ], m = 1 - l, y = h.length; m = 0;)
                            delete f[i[g]];
                }
                return j.call(n, o, f);
            };
            e = p({
                "" : e
            }, "");
        }
        return e;
    };
}();
Sujay
  • 2,198
  • 23
  • 32
  • Add this to the top of your js file. It'll use the native version if avl. If not, it uses the custom implementation. – Sujay Feb 24 '11 at 07:24
  • Does it mean that JSON isn't implemented in IE and we need to code its parsing functionality by hand?! – Konrad Viltersten Nov 27 '12 at 14:36
  • Also, what do the parentheses at the end fill as a function? I'm not clear on why they are needed... – Konrad Viltersten Nov 27 '12 at 14:38
  • @KonradViltersten Legacy browsers don't have it implemented. Modern browsers do. I don't understand your 2nd question. Please be clear as to which part you're not clear about. – Sujay Nov 28 '12 at 10:09
  • If you scroll all the way down in your code example, you'll see that there's `();` at the very end. I would expect just a curly brace, since you're declaring a function. What do I miss? – Konrad Viltersten Nov 28 '12 at 10:24
  • @KonradViltersten I just noticed that. Its definitely a copy-paste error. I'm very sorry. But I no longer have the actual source. I myself make use of the [JSON3](http://bestiejs.github.com/json3/) polyfill & recommend you to do the same. – Sujay Nov 29 '12 at 09:48
  • JSON3? That's a new one. Didn't hear about it until now. – Konrad Viltersten Nov 29 '12 at 19:25
1

To make your function works better in IE import JSON2 parser code in your file, as IE by default does not support JSON.Stringify().

It can be found here

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
Mghost.friend
  • 382
  • 1
  • 3
  • 5
0

In IE open the compatibility view settings and remove the localhost from the listbox for "Websites you have added to Compatibility View". It worked for me.

Sanjay
  • 39
  • IE7 doesnt support JSON `stringify` and `parse` methods, so OP really needs to use JSON2 library. Your answer is relevant for IE8 and later. – vittore Oct 11 '12 at 11:22
  • hundred times I fixed such JSON and other js errors in IE by manipulating compatibility settings, either removing sites frim compatibity list or disabling standard Microsoft list and intranet checkboxes. This comment makes sense, don't hesitate improving it and voting for it – moudrick Dec 11 '14 at 08:03
0

JQuery 2.x is no longer compatible with IE 6-8. JQuery 2.0 beta 2 release notes

I know the main question is in regard to older versions of JQuery, but this was causing the error for me. I installed JQuery 1.x, which is API-compatible with JQuery 2.x, and added the following detection code:

<!--[if lt IE 9]>
 <script src="js/jquery-1.11.1.min.js"></script>
<![endif]-->
<!--[if gte IE 9]>
 <script src="js/jquery.min.js"></script>
<![endif]-->
arafeandur
  • 196
  • 1
  • 5