简体   繁体   English

Rails JSON响应未呈现

[英]rails json response not rendered

The controller should show a different page depending on the button pressed using AJAX, via a POST request 控制器应通过POST请求显示不同的页面,具体取决于使用AJAX按下的按钮

# POST /site_device/1
def show_page
     @site_device = SiteDevice.find(params[:id])
     respond_to do |format|
         format.js
         format.json { STDERR.puts "JSON!"; render 'show_page.js.erb'  }
    end
end

The controller code #show page runs, and calls the json view template (below) 控制器代码#show页面运行,并调用json视图模板(如下)

<% STDERR.puts "got_here" %>
$('#device-data').html("<h1>Foo!</h1>" ).trigger('create');

We know the above code is being called as the STDERR lines are being output on the server. 我们知道上面的代码在服务器上输出STDERR被调用。

On the browser (Chrome) we can use the console to test whether the code in the view template runs correctly – and it does: Entering $('#device-data').html("<h1>Foo!</h1>" ).trigger('create'); 在浏览器(Chrome)上,我们可以使用控制台测试视图模板中的代码是否正确运行-并且可以这样做:输入$('#device-data').html("<h1>Foo!</h1>" ).trigger('create'); into the Google Chrome console results in the html being re-rendered with “Foo!” in the right place. 插入Google Chrome控制台会导致在正确的位置用“ Foo!”重新渲染html。

Here however is the perplexing bit. 然而,这是困惑的位。 The code that is being executed on the server doesn't appear to be received at the browser, or if it is, it is not executing, or not rendering 在服务器上执行的代码似乎没有在浏览器中接收到,或者正在执行,或者未呈现。

Has anyone else had any similar problems/issues? 还有其他人有任何类似的问题/问题吗?

To hazard a guess, if your ajax function has dataType: 'JSON' it will receive but not execute the response. 冒昧地猜测一下,如果您的ajax函数具有dataType: 'JSON' ,它将接收但不执行响应。 If you set dataType: 'script' the response will be executed. 如果设置dataType: 'script' ,将执行响应。

Please post your ajax function if this isn't the answer. 如果不是答案,请发布您的ajax函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM