Posts Tagged sillyness

Silly Access-Control-Allow-Origin Workarounds

“Origin null is not allowed by Access-Control-Allow-Origin” was the error I was getting when looking at a local html/JS powered page on my desktop, and I tried to do a getJSON request in jQuery.

It’s a well known problem – but also quite annoying, mainly because the fixes were generally of the form “change your endpoint to have a callback”, or “modify your code so it does X, even though that means you have to change Y and Z too”.

So I looked around, and was about to default back to my old plan of dumping everything into an array when I realised that because my JSON is constant, I can just have it as an excessively long string, and use parseJSON to parse it.

And so I ended up with something like this:

data.js consisted solely of var json="[{...}]";

logic.js called $.parseJSON(json)

And my main file had

<script type="text/javascript" src="mathcards.js"></script>
<script type="text/javascript" src="data.js"></script>

But it worked. And with only an extra line (the extra script/data.js line – possibly even a net drop since I got rid of the success function), I’m calling it a success.

Then again, it’s 12:30 in the morning after the facebook hackathon (which was pretty awesome, even if MathJax broke during my demo because I forgot to include the $ signs for inline math).

Also, JSONLint is my new favorite utility – not only does it validate your JSON, but it also pretty-print formats the long string into a form that looks a lot nicer to save.

, ,

No Comments