JSON.stringify and JSON.parse not working in IE9? - javascript

I'm using JSON.Stringify and JSON.parse everywhere and it works fine with Firefox. It's working no more with IE9 nor does it work in IE8. What can I do?

JSON.stringify starts with a lower-case s. Both stringify and parse are available in IE8+, but only in standards mode.
Prepend your document with <!DOCTYPE html> if you're currently using quirks mode. Also, watch the capitalization of the JavaScript methods you call - all built-in ones start with a lower-case character.

why do you want to depend on the browser having the object instead just include the script file by Douglas Crockford.. You can find the minifed file here: http://www.json.org/js.html
Once imported you dont have to worry abt the method existing in a browser.

For an alternative, in a scenario where you might need to run in strict mode for whatever reason (I have another library that includes "use strict"), you can look here: https://github.com/douglascrockford/JSON-js. I modified this to check first if JSON is undefined, and only generate the function JSON.parse if it is:
if (typeof JSON === "undefined") {
var JSON = {
parse: <insert value of json_parse from library here>
};
}
My issue was application code not working in IE9 (strict mode being used by a participating library, I believe). That solved the problem for me.

the mere issue is, that sending UTF-8 headers will invalidate the JSON (IE doesn't/didn't like that). as the issue is described, that might still apply for IE9... once wrote a how to, a few years ago. adding JSON support to a browser which can parse native JSON is probably not the optimal solution, since it produces useless overhead - only because failing to deliver the JSON in the expected format.

Related

Weird 'ChildNodes of undefined' error in IE8 with an AngularJS application

TypeError: Unable to get value of the property 'childNodes': object is null or undefinedundefined
After making a long list of modifications to my application in order to support IE8, including:
running all of the views in their compiled form through W3C validator, setting up xdomain.js proxy to support CORS API calls, making some general restructures, etc.
I was very disappointed to find out that IE8 still throws this weird error at me, while IE9 works perfectly fine.
Making changes to the Angular-seo package to prevent it from running when the client is an IE browser.
Any ideas on what can it be?.
Make sure that all your tags are closed properly. I just spent hours trying to figure out what the problem was, until I noticed this in my code:
<span>some text<span>
I finally realized I didn't close the <span> tag properly. After that everything just worked.
Without the code you are running it is a bit difficult. However there is a command to use for debugging. First you need to identify which variable might not contain an object [i.e.
"object is null or undefined"].
For example, parent, then you can use
//next look to see if parent is something
if('undefined'==(typeof parent)) alert("variable empty:parent");
Once you find something that is empty that you are expecting to be an object then you can go trace back from there. Also use a browser debugged tool, to identify the line number of the error.
Often if using the child nodes, you may not have the right level or you need to access as an array i.e. you need something like.
parent.childNodes[0].childNodes[0].value
In IE you are also dealing with unsupported functions. So getElementById will work but some other similar ones do not. Again typeof can be useful.
//next ensure function supported
if( 'undefined'==(typeof document.getElementsByClassName) ){
alert("Not Supported"); // notice ^ no () required here
//...add code to handle differently when not supported
}
This may reveal if you can use a function
IE8 is so old and non-standards compliant it doesn't support childNodes[]. http://quirksmode.org/dom/core/#t70

json2.js JSON.stringify() generates stack overflow when handling a little bigger value

JSON.stringify(value) can not handle just a little bigger value. It makes "stack overflow" error. Is there a workaround?
json2.js is from https://github.com/douglascrockford/JSON-js
I am running my JavaScript programs on Windows DOS box through cscript.exe (WSH), which is JScript v5.8 on Windows 7 and Windows 8.
I can't find the way to use the, it is said, built-in JSON in JScript v5.8.
So I include the json2.js and it works!
It works but not very well. JSON.stringify(mydata) generates "Stack overflow" when mydata is just a bit bigger structure.
The way I include json2.js is:
open json2.js, read entire json2.js into a string ss, eval(ss). Then JSON becomes available and it works.
By the way, I can't see JSON in cscript.exe JScript v5.8, how to enable it?
Script engine available via WScript/CScript is the one used in IE7 - so no JSON object as you've already found (and no way to somehow "enable" it).
Most likely reason of stack overflow is attempt to serialize recursive structure instead of proper tree. I don't believe JSON2.js supports such cases. Simple structure with self reference is below (normal JSON.stringify fails on it)
var node = { a:"text"};
node.next = node;

Check webkit version with javascript embedded pywebkitgtk?

Apparently the best way to check the version of webkit is
/AppleWebKit\/([\d.]+)/.exec(navigator.userAgent)
(as seen here and a few other places). What is this /AppleWebKit/, a hidden variable, a constant, a regex?
I noticed it worked fine in the browser, but would not work in an embedded pywebkitgtk, it returns null. Is there something missing in the embedded webkit?
/AppleWebKit/([\d.]+)/ is a regular expression which navigator.userAgent is checked against.
The navigator object returns information about the browser you use (name, version, etc). So I guess this does not exist in pywebkitgtk, as it's only a framework for WebKitGtk, not a browser by itself.

Type coercion bug in IE8 RegExp.exec()?

I don't know if this is a know problem in IE8, but I can't really find any info on it.
// The regex can vary but has to have a non-matching group defined:
var re = /^(\s)?[\d]+$/i;
// We call it with a string...
re.exec("2");
// We call it with a number...
re.exec(2);
Firefox and Chrome (can't try it in Opera right now) have no problem with either calls. But on IE8 the second call fails with an "Object does not support that property or method".
Is this a known bug or something?
I saw the same issues in an Ext JS 4 application. Lots of things were failing as Ext JS appears to pass numbers in the exec() method at times. The issue turned out to be a third party library SyntaxHighlighter. Removing this reverted the default IE8 behavior and re.exec(2); worked.
I'd suggest cutting down the external JS that you include in your app until you find the culprit.
Since exec takes a string I would make sure you are passing a string. By passing a number in I would say you are trying to count on grey areas of the way browsers implement javascript.

How to get the JSON-string value of a complex object?

I use jQuery to get jsonData from the server. I don't know what the data is. I would like to get the string value of the JSON object and write it in a textarea tag so I can debug. How? Please and thank you.
P.S. I am using .NET MVC and jQuery
You could convert it back to JSON:
$("the_textarea").val(JSON.stringify(data));
It's actually quite readable.
Or use a tool like Firebug to dump it:
console.log(data);
If you plan on using JSON.stringify you should include json2.js to support older browsers.
Another route to solving your problem is to use firebug. It can show you each request along with the incoming and outgoing data.
for most modern browsers (except IE <= 7) you can use JSON.stringify(object)

Categories