true, "data-type" => :json, :id => "answerForm") do %> 在某-6ren">
gpt4 book ai didi

jquery - rails - JS 回调未使用远程 JSON 形式执行 (jQuery UJS)

转载 作者:数据小太阳 更新时间:2023-10-29 08:05:05 25 4
gpt4 key购买 nike

在 .erb View 中考虑以下形式:

<%= form_tag(harvester_next_url, :method => "post", :remote => true, "data-type" => :json, :id => "answerForm") do %>
<div id="dynamicFormContent"></div>
<% end %>

在某些情况下,我想在 next 方法中发出一个 JS 回调render :js => "window.location = '#{definitions_path}'"

但是,这不会在浏览器中执行。响应在正文中有“window.location...”并且 content-type 设置为 text/javascript,请求的 accepttext/javascript也是。

Rails 版本是 3.2.8。

一些附加信息:我不想将表单操作切换为使用 JS 格式,因为 JS 回调代码只是来自 Controller 的可能响应之一。还有一次,我需要从中获取一些 JSON 来处理。

问题:是否有可能不使用客户端重定向(即从 JSON 响应获取 window.location)等? AFAIK,这对非远程表单有效。

ADD 当 XHR 完成时,它以 status = "parsererror" 结束。显然,这是因为 jQuery 试图将响应解析为 JSON,即使它是 text/javascript。问题仍然悬而未决。

最佳答案

监听回调并在客户端进行重定向:

$('#answerForm').on('ajax:success', function(data) {
if (data.url) {
window.location = data.url;
} else {
// do something else...
}
})

# -- controller code
render json: {url: definitions_path}

关于jquery - rails - JS 回调未使用远程 JSON 形式执行 (jQuery UJS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13515768/

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