Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Rails以HTML形式返回GET响应,而不是重定向_Javascript_Jquery_Ruby On Rails_Ruby - Fatal编程技术网

Javascript Rails以HTML形式返回GET响应,而不是重定向

Javascript Rails以HTML形式返回GET响应,而不是重定向,javascript,jquery,ruby-on-rails,ruby,Javascript,Jquery,Ruby On Rails,Ruby,运行以下代码表明控制器和视图正在正确地接收和解析JSON,并返回HTML。(这可以通过回拨功能中的警报看到。) 但是,我希望浏览器重定向到/json_test/main,而不仅仅是接收响应中的HTML 控制器: class JsonTestController < ApplicationController def main @postData = params[:scores] end def test end end class JsonTestControl

运行以下代码表明控制器和视图正在正确地接收和解析JSON,并返回HTML。(这可以通过回拨功能中的警报看到。)

但是,我希望浏览器重定向到/json_test/main,而不仅仅是接收响应中的HTML

控制器:

class JsonTestController < ApplicationController
  def main
    @postData = params[:scores]
  end
  def test
  end
end
class JsonTestController
main.html.erb:

<h1>JsonTest#main</h1>
<p>Find me in app/views/json_test/main.html.erb</p>

<% @postData.each do |key, value| %>
    <p><%= key %> : <%= value %></p>
<% end %>
<script type="text/javascript">
var postUrl = "<%= json_test_main_path %>";
var postJson = JSON.parse('{ "scores" : { "player1": 5, "player2": 6 } }');

 $.get(postUrl, postJson, function(data, status, xhr) {
    alert("Data: " + data + "\nStatus: " + status);
}, "html");

</script>
JsonTest#main
在app/views/json_test/main.html.erb中查找我

:

test.html.erb:

<h1>JsonTest#main</h1>
<p>Find me in app/views/json_test/main.html.erb</p>

<% @postData.each do |key, value| %>
    <p><%= key %> : <%= value %></p>
<% end %>
<script type="text/javascript">
var postUrl = "<%= json_test_main_path %>";
var postJson = JSON.parse('{ "scores" : { "player1": 5, "player2": 6 } }');

 $.get(postUrl, postJson, function(data, status, xhr) {
    alert("Data: " + data + "\nStatus: " + status);
}, "html");

</script>

var postrl=“”;
var postJson=JSON.parse(“{”分数“:{”player1:5,“player2:6}”);
$.get(postrl、postJson、函数(数据、状态、xhr){
警报(“数据:+数据+”\n状态:+状态);
},“html”);

如果您的目标是将用户重定向到另一个页面,那么执行AJAX请求有什么意义呢

没有意义

尽管如此,您仍可以在$.get中添加一个成功回调,用于重定向浏览器

  .done(function() {
    window.location.href = "http://stackoverflow.com";
  })

你为什么期望发生重定向?您的代码中没有任何内容我认为默认设置是呈现URL中指定的视图(在本例中为/json_test/main),如何在不使用GET请求的情况下使用必要的参数重定向到此页面?(如果这是一个愚蠢的问题,我很抱歉。)您的AJAX请求是$.GET。如果没有某种请求,就没有AJAX操作。我想你还不确定你想要什么。