I want to be able to read the response headers of my app with jquery, specifically, I set the header 'X-Messages' to a string representation of rail's flash messages hash.
My question is similar to this one: https://stackoverflow.com/a/2729454/103739
I have one major difference however, that this isn't a JSON response. I just want to read the headers returned in my HTML page with jQuery, and log something for each item in the JSON object.
Here is what I'm doing in my controller:
class ApplicationController < ActionController::Base
after_filter :flash_to_headers
# ...
def flash_to_headers
response.headers['X-Messages'] = flash.to_hash.to_json.to_s
end
end
Then here is where I'm stumped, I want to get this JSON object in when the page loads and just display say a regular alert
, or console.log
:
$(document).ready(function() {
var messages = document.getResponseHeader('X-Messages');
console.log(messages);
});
Okay, now obviously getResponseHeader()
, is not a function on document
, but how can I accomplish this?