gpt4 book ai didi

javascript - jquery $.post 之后的 Rails redirect_to

转载 作者:行者123 更新时间:2023-11-29 19:33:28 25 4
gpt4 key购买 nike

为什么我的浏览器在我提交时不转到新页面?

在我看来,我有一个按钮可以将数据发布到我的 Controller 中并在其中执行操作。

我的 CoffeeScript :

$.post "/token/create", json

TokenController#create

redirect_to root_path, notice: 'Parser was successfully created.' 

日志文件看起来有效:

Started POST "/token/create" for 127.0.0.1 at 2014-10-08 10:27:39 -0400
Processing by TokenController#create as */*
...
Redirected to http://localhost:3000/
Completed 302 Found in 37ms (ActiveRecord: 10.0ms)


Started GET "/" for 127.0.0.1 at 2014-10-08 10:27:40 -0400
Processing by VisitorsController#index as */*
...
Completed 200 OK in 2891ms (Views: 2886.6ms | ActiveRecord: 3.0ms)

但是我所在的页面没有变化。如何使页面转到我在“redirect_to”行中指定的任何页面?

最佳答案

您可以执行以下操作:

def create
# do some stuff
if request.xhr? # test if the request is an AJAX call
render js: "document.location = '#{root_path}'"
else
redirect_to root_path, notice: 'Parser was successfully created.'
end
end

您想在 AJAX 情况下也使用 flash 吗?检查这个答案:https://stackoverflow.com/a/18443966/976775 (如果对您有帮助,请对他的回答投赞成票)

关于javascript - jquery $.post 之后的 Rails redirect_to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26259627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com