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

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)

Related

DataCloneError in firefox when posting to web worker

I am working on a helper library called Ozai to make web workers easier, but am running in to a problem in firefox. I create a web worker from a URL Blob and attempt to post this payload to it:
msg = {
"id":"0fae0ff8-bfd1-49ea-8139-3d03fb9584e4",
"fn":"fn",
"args":[100,200]
}
Using this code:
worker.postMessage(msg)
But it throws a DataCloneError exception. It looks like Firefox's implementation of structured cloning is failing on a very simple object. The code runs without problems on Chrome and Safari, but fails in the latest version of Firefox. Am I missing something here? How do I get around this (preferably without stringifying the payload)?
Here's a fiddle: http://jsfiddle.net/V8aCy/6/
And a pic of Firelord Ozai:
You're trying to call postMessage with an object that has a property referencing arguments. That doesn't work because data has to be transferable, which means either fully JSON-serializable or implementing Transferable (e.g. ArrayBuffer), which arguments is not.
Use Array.prototype.slice.call(arguments, 0) to convert arguments into an array, which can be serialized (cloned) if the contents are OK.
Corrected fiddle.

Best way to urldecode (php) in NodeJS

So I am trying to decode a string that was previously urlencoded with php in Node. About a month ago I had it working with:
querystring.unescape(str.replace(/\+/g, '%20'));
Then it just stopped working - not sure if it was some Node upgrade or what. After playing around it seems I can just use 'unescape()' but I am not sure it if it's foolproof yet.
unescape(str.replace(/\+/g, '%20'));
My question is what is the best way and has anyone else noticed this issue. Note that the first line works with simple strings but breaks down with odd characters - so maybe some encoding issue I am not seeing.
Here's a string:
%E6.%82%CCI-T%8C%01+A
Now go to http://www.tareeinternet.com/scripts/unescape.html and decode it. That is my original (it's an RC4 encrypted string). I want Node to return that string.
If you just use the unescape function that's built in into Node.js, your result should be what you want.
Using Node.js 0.10.1 and running
unescape('%E6.%82%CCI-T%8C%01+A');
on the interactive shell, I get
'æ.ÌI-T\u0001+A'
as result which looks pretty much like what you would like to get.
Hope this helps :-)

How to decompress gzip xhr response in javascript

I have a gzipped response from a web request that I need to decompress in JavaScript (actually, in the success function of an AJAX call - my code is running in a headless browser, and doesn't have the built-in gzip processing support offered by a full browser). I've been looking around for an answer but I'm a bit stumped.
Ideally, what I'd like to have code for is:
var my_decompressed_string = someGzipDecompressor(xhr.responseText);
I found, what I thought was an answer at JavaScript implementation of Gzip but that may not be the answer I was hoping for. When trying to use the mentioned jsxcompressor library by way of the following code snippet
var my_decompressed_string = JXG.decompress(xhr.responseText);
... I get ...
TypeError: 'undefined' is not an object (evaluating '(new JXG.Util.Unzip(JXG.Util.Base64.decodeAsArray(str))).unzip()[0][0]')
Looking at that function in more detail, if I break up the code being executed by the decompress() function, I get what I assume is something good returned by the inner part ...
JXG.Util.Base64.decodeAsArray(xhr.responseText)
... but undefined returned for the outer part...
JXG.Util.Unzip( ... )
So, that may be a dead end of course, but if anyone knows a way that my original question should be possible, or has had any better luck with jsxcompressor.js I'd appreciate it.
Of course, I can force my AJAX request to return a 'deflate' response but the size of the page (for which I have no control over) is pretty large and requesting gzip is an attempt at speeding up my page load time.
jsxcompressor.js requires base64 encoded string to decompress, you should use:
var my_decompressed_string = JXG.decompress(btoa(xhr.responseText));
if your headless browser does not support btoa then you should use a base64 encoding library, if your node or iojs there are plenty of base64 npm packages.

How to create a IXMLDOMDocument2, or otherwise add one of it's IXMLDOMElements to a [Object, Document]?

I'm getting an HIERARCHY_REQUEST_ERR (3) in IE9 only when I try to add an element from one document to the other.
I'm creating my document as follows:
var xmlDoc = document.implementation.createDocument("","root",null);
and trying to add to it as follows:
$(otherDoc).find("selector_for_nodes_I_want").each(function(){
$(xmlDoc).find("root").append($(this).remove());
});
Firefox, and Chrome are fine with this, but IE9 doesn't like it. The idea is that xmlDoc is a filtered version of the otherDoc, which I will store as a string in sessionStorage. I'm hoping to save XMLHTTPRequests this way. I'm not too keen on the option of converting otherDoc to a string first, then converting back via $.parseXML(). I'm hoping for a better option, or if I'm thinking about this all wrong, I'd hear that argument as well.
Thanks.
In lieu of an answer for the OP, is there anyone with a decent explanation as to why it is necessary to use domParser() or $.parseXML(jqXHR.responseText) when the response header is xml already: Content-Type:text/xml; charset=utf-8. Why does IE9 guess that I want to use this ridiculous IXMLDOMDocument2 implementation to parse my response?

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

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.

Categories