Created
March 21, 2013 13:48
-
-
Save mlangenberg/5213128 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# When doing Cross Origin request from Internet Explor 8 & 9 | |
# the XDomainRequest object must be used. This object does not | |
# give access to the response status code and *discards* the | |
# responseText for responses with status code other than 2xx. | |
# | |
# This Rack Middleware wraps the original response body and | |
# status code in a JSON object and always returns a 200 OK. | |
class Rack::Ok | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
status, headers, response = @app.call(env) | |
if x_domain_request?(env) | |
wrapped_response = MultiJson.dump({ | |
:status => status, | |
:response => response.body | |
}) | |
headers['Content-Type'] = 'application/json' | |
[200, headers, [wrapped_response]] | |
else | |
[status, headers, response] | |
end | |
end | |
# TODO check params if response must be 200 | |
def x_domain_request?(env) | |
true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment