[英]Rails 5: Respond to AJAX call with HTML (instead of javascript)
(發布版本5.1.2)
我想使用HTML而不是JavaScript來響應AJAX,其原因與本堆棧溢出問題中概述的原因大致相同。
但是,我實際上無法使其工作。
我使用帶有remote: true
form_for
通過AJAX發送請求。 請注意,我還嘗試了data: {type: :html}
。
我的控制器動作具有線render layout: !request.xhr?
並具有關聯的視圖。 我要將此視圖發送回客戶端。
客戶端代碼:
$("form").on('ajax:success', (e, data, status, xhr) ->
console.log xhr.responseText #using console.log data produces same result
)
得到:
Turbolinks.clearCache()
Turbolinks.visit("http://localhost:3000/...", {"action":"replace"})
HTML在哪里?
除非我完全誤解了您想做什么,否則應該就是您要尋找的東西:
Javascript :
$.ajax({
// remember to add this route to your routes file
url: "products/ajax_render",
success: function(data){
$('.some_div').html(data['html'])
},
});
Ruby on Rails :
def ajax_render
# render some view and store it in a variable
html = render "products/your_view"
# return it inside the json response
render json: { html: html }
end
我想念什么嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.