gpt4 book ai didi

ruby-on-rails - jQuery ajax 请求未触发 Rails Controller 的 JS 响应?

转载 作者:行者123 更新时间:2023-12-03 21:29:45 25 4
gpt4 key购买 nike

我有一个标准 Controller ,设置为响应 HTML、JS 和 JSON 请求:

def picture
@picture = Picture.new params[:picture]

respond_to do |format|
if @picture.save
format.html do
logger.debug "In the HTML responder"
redirect_to @picture
end
format.json { render json: @picture, status: :created, location: @picture }
format.js { render :nothing => true }
else
# you get the idea
end
end
end

现在我尝试使用 $.ajax 函数向该 Controller 发送请求(在这种特定情况下我无法使用 :remote => true - 我正在尝试使 ajax 文件上传工作)。

$.ajax({
url: $("form#new_picture").attr("action"),
type: "POST",
data: formdata,
processData: false,
contentType: false
});

问题是由于某种原因我的请求被视为 HTML 请求。我如何告诉 Rails 我想要 JS 响应?

顺便说一句,我在我的项目中使用 jquery_ujs,因此我可以在必要时访问它提供的方法。我在 JS 方面还不够好,无法调整它来完成我在这里需要的事情。

最佳答案

这个解决方案对我不起作用(rails 3.1 + Coffeescript)。经过一番搜索,我找到了很好的方法,我想分享:

只需在网址末尾添加“.js”即可。如此简单...;-)

关于ruby-on-rails - jQuery ajax 请求未触发 Rails Controller 的 JS 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6919801/

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