开发者

'JSON' is undefined error in IE only

开发者 https://www.devze.com 2023-02-12 18:11 出处:网络
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 wor开发者_StackOverflow中文版ks fine in FF, & Chrome, but not IE8.I get an error:\

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 wor开发者_StackOverflow中文版ks 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


Use json2 for a consistent cross browser implementation.

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


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!


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;
    };
}();


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


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.


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]-->
0

精彩评论

暂无评论...
验证码 换一张
取 消