Jarod Watkins
2012-Apr-16 15:05 UTC
Same controller, two contexts return different request headers
Hello, I have one controller that has two routes. One is a "direct" route and the other is a nested resource under another object. The two URLs are: http://example.com/controller http://example.com/nested/controller I have an AJAX form that submits data to a method via the GET method and then renders a JS ERB template to execute some Javascript and update a div. This works as expected with the nested route. However the bare or "direct" route''s request headers that are sent to controller from the AJAX call are "text/html,application/xhtml +xml,application/xml;q=0.9,*/*;q=0.8" vs the proper "*/*;q=0.5, text/ javascript, application/javascript, application/ecmascript, application/x-ecmascript". I have tried different ways to force the client to send the request headers as javascript: - Setting data-type attribute to script in the form - Setting the headers["Content-Type"] to "text/javascript; charset=utf-8" in the controller - Adding :format => :js in the URL path helper method I know I have tried more but I cannot remember all that i have tried in the last two days trying to figure this out. What is weird is how the same code works for the nested route but does not work for the bare version. I am using Rails 3.1.3 with Ruby 1.9.3p0. Thanks for any help, Jarod -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Draiken
2012-Apr-17 11:07 UTC
Re: Same controller, two contexts return different request headers
This is probably a problem with the request and not the response. Rails will give you what you ask on headers. So if your AJAX call is asking for html, it''s giving HTML and not JS. Check if you''re really asking for javascript on the ajax call''s headers, and please post how you''re making this calls. On Apr 16, 12:05 pm, Jarod Watkins <jwatk...-BlZJrTnzxPPQT0dZR+AlfA@public.gmane.org> wrote:> Hello, > I have one controller that has two routes. One is a "direct" route and > the other is a nested resource under another object. The two URLs are: > > http://example.com/controllerhttp://example.com/nested/controller > > I have an AJAX form that submits data to a method via the GET method > and then renders a JS ERB template to execute some Javascript and > update a div. This works as expected with the nested route. However > the bare or "direct" route''s request headers that are sent to > controller from the AJAX call are "text/html,application/xhtml > +xml,application/xml;q=0.9,*/*;q=0.8" vs the proper "*/*;q=0.5, text/ > javascript, application/javascript, application/ecmascript, > application/x-ecmascript". > > I have tried different ways to force the client to send the request > headers as javascript: > > - Setting data-type attribute to script in the form > - Setting the headers["Content-Type"] to "text/javascript; > charset=utf-8" in the controller > - Adding :format => :js in the URL path helper method > > I know I have tried more but I cannot remember all that i have tried > in the last two days trying to figure this out. What is weird is how > the same code works for the nested route but does not work for the > bare version. I am using Rails 3.1.3 with Ruby 1.9.3p0. > > Thanks for any help, > Jarod-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.