Sunday, March 22, 2015

the joy of hack: diy "jsonp-ishness"

For a side project I've been working on, the library relies on a series of static JSON files to describe parts of the layout. Unfortunately, that part of the project wasn't working when put on the client's webserver. Investigating, I saw that my browser could successfully request the image files in the same folder, but the JSON was all 404'ing.

I asked for a screenshot of the folder listing, and the JSON files were present but listed as type "unknown". This was some kind of custom webserver, so I'm guessing it was configured to ignore filetypes it didn't know about --  including JSON, apparently.

The fix was straightforward-ish; hack the JSON loader for this set of files so it pulled value from a map hard-coded in the script itself. It was a sparsely populated map, only 4 or so of the 100+ pages had JSON that was anything but the empty array [], so the hack function returns an empty array if it couldn't find a value in the map, rather than me having to populate the map with 100 blank entries.

So for this arrangement, this technique was probably more efficient anyway. For that matter, this meant I could now pull up the project's index.html from the local filesystem and have it all work, which wasn't the case when the files were in external JSON.

So a win all around. Nothing too ground-breaking, just a kind of poor man's adhoc retelling of JSONP...

No comments:

Post a Comment