Evidently jQuery has made me dumb.
I’ve got a local url that serves up raw JSON, and I can’t figure out how to consume that json from within my method without using jQuery.
Here’s how I know to do it WITH jQuery
var myJson;
$.getJSON('/local/path/to/json', function (data) {
myJson = data;
});
// Now I can use myJson in a method.
To retrieve the JSON string from a server use
XMLHttpRequestobject as described in this reference:http://developer.mozilla.org/en/XMLHttpRequest
You’ll find that it’s quite involved with all the unseen things you need to account and check for. Thus libraries like jQuery.
To convert the JSON string to a javascript object, use
JSON.parse(). Here’s the reference:http://developer.mozilla.org/En/Using_native_JSON
Here’s an example:
EDIT #2: Thanks for editing in this example, Chase. A word of warning. It is not a good idea to make the
open()method a synchronous call by usingfalsein the 3rd parm. AJAX is intentionally designed for asynchronous use, and to make a synchronous call invites lock ups. As one who used to think there was a place for synchronous calls, I now find there’s always a better way to get it done asynchronously. Word to the wise.