【发布时间】:2016-12-21 20:28:10
【问题描述】:
我们刚刚使用 SSL 证书保护了我们的应用程序。事实证明,迁移到 HTTPS 比我们想象的要复杂得多,我们目前正在整理一些由此产生的错误。
我们在 CoffeeScript 中有一个 AJAX 调用,rails 通过渲染部分的 html 来响应。这在开发中完美运行。
CoffeeScript:
coffee_method: (pos, uid) =>
$.ajax '/contoller/test/',
type: 'POST'
data:
pos: pos
uid: uid
success: (data) ->
$('#result-div').html(data) #Populates side menu with _next_destination_menu content
error: ->
alert 'How embarassing! Something went wrong - please try your search again. '
控制器:
def test
... #do some stuff
puts "format requested: #{request.format}"
puts "format url_encodeded: #{request.format.url_encoded_form?}"
render partial: 'trips/_test' #app/views/trips/_test.html.erb
end
但是,在生产中,我们收到以下错误:
ActionView::MissingTemplate (Missing partial trips/_test with {:locale=>[:en], :formats=>[:url_encoded_form], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
经过一番挖掘,我发现生产中的请求是一种不同的格式。使用控制器中的那些puts 行进行调试,结果如下:
生产:
format requested: application/x-www-form-urlencoded
format url_encodeded: true
发展:
format requested: */*
format url_encodeded: false
处理此问题的最佳方法是什么。我:
- 更改 CoffeeScript 中每个 AJAX 调用的内容类型?
- 向控制器添加
respond_to...format.url_encoded_form {render partial: trips/test}?
后者似乎我会重复代码,因为我想呈现相同的部分,而不管请求的格式如何。我尝试了format.all {...},但遇到了同样的问题。感谢任何最佳做法!
更新:
直接指定响应类型会给我同样的缺失模板错误:
respond_to do |format|
format.html {render partial: 'trips/test'}
format.json {render partial: 'trips/test' }
format.url_encoded_form {render partial: 'trips/test'}
end
更新 2:
localhost 和 production 的请求标头相同,内容类型为 application/x-www-form-urlencoded,即使 format.url_encoded_form? 返回 false。
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
Connection:keep-alive
Content-Length:37
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
【问题讨论】:
-
您是否使用了可能会改变请求的代理(如 nginx?)?
-
@NickTomlin - 我不相信我们正在使用代理,但我们正在使用 AWS CloudFront 进行 Web 分发。
标签: ruby-on-rails ajax coffeescript partial-views content-type