Skip to content

Instantly share code, notes, and snippets.

@mlangenberg
Created March 21, 2013 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlangenberg/5213128 to your computer and use it in GitHub Desktop.
Save mlangenberg/5213128 to your computer and use it in GitHub Desktop.
# 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