Thursday, February 10, 2011

Sundae.js - getting JSON files

As with my previous post,

Sundae.js uses a JSON file to control its test cases. I define a rigid API with the expected test case format, and a user fills out a JSON file at least suppling the required field. A sample of my valid JSON file can be found here.

So in order to get a JSON file, or the contents of any file as a string you need to create a XMLHttpRequest() object. The Mozilla MDC provides detailed explanations of its attributes and expected uses and cautions.

Here are the steps I followed to get this done for Sundae.js:

  1. Make a XMLHttpRequest() object.
  2. Set its open parameters
  3. Set its onload()
    1. Very handy for managing callback functions
    2. Also handy for using the objects responseText
  4. Set its send to null
As usual here is a pastebin of getJSON, and there you have it. Alternatively if you aren't using valid JSON for JSON.parse(responseText) of your file, you should use eval("(" + responseText + ")").

No comments:

Post a Comment