eligrey/pmxdr — pull request #8 by fidian (2013-06-24 17:06:22+00:00)
Title: Handle Internet Explorer's status=1223 quirk + restructuring

Internet Explorer has a quirk when it receives a 204 No Content reply from the server and it expected some information. For POST (and I believe GET), the XMLHttpRequest.status will = 1223 instead of 204. It also has an incorrect statusText and one is unable to access the response headers.

To combat this problem, the best solution I have is to fire off an OPTIONS call to the server, like how other browsers do it. The OPTIONS call must not come after the GET/POST that resulted in a 204 No Content response because our last request may have altered the resource and potentially removed that resource or it now will get handled differently. So the OPTIONS call has to be done up front in case of the 204.

An OPTIONS call is issued before our request and the CORS-related headers are now saved. If the response comes back with a 1223 status, we override the response's status, statusText and the use the CORS headers from the OPTIONS call to determine if the request was allowed.

Included in this pull request is a lot of formatting changes. I hope that I don't offend by making them. I realize that most of the changes are not necessary for fixing the bug. I'm in the habit of breaking complex code out into functions and found it far more readable and hopefully more maintainable. If the size is worrying, the minified code (with uglifyjs -m -c for super small code) went from 4387 bytes to 5693 byes.

* Consistent indentation
* Passes cleanly with jslint with only the `eval()` lines using special handling and the necessary `__defineGetter__()` call
* Replaced JSON.parse with a version that does not use `eval()`
* Using an `eval()` version of the JSON object so it gets through validators easier (eg. jslint)
* Broke code into functions and commented them
* Made a reusable `makeRequest()` function because now we get to make two requests
* Handle IE's 1223 status code
* Handle searching the headers consistently
* Fix potential bugs for if trying to GET on a resource and the allowed methods included 'FORGET' (it would find "GET")
* XMLHttpRequest.getResponseHeader() can throw at rare instances - wrapped in try/catch
* Wrapped `JSON.parse` in try/catch

Please let me know what you think of the changes. I'd love to get feedback.