gpt4 book ai didi

jquery - Rails 3 respond_to 不响应

转载 作者:行者123 更新时间:2023-12-01 06:05:41 25 4
gpt4 key购买 nike

我试图解决这个问题,但我需要帮助!

我正在使用rails 3和jquery,我包含rails.js并使用gem'jquery-rails'来创建它 - 但仍然;使用 $.ajax({url: self.attr('href')}) 调用 Controller 时; Controller 内的 respond_to :js 只是使用 html 进行响应。

Controller :

respond_to :html, :js

# GET /customer/new
def new
@customer = Customer.new

respond_with(@customer)
end

application.js:

$.ajax({url: self.attr('href')});

起初我以为这是设置正确 header 的问题,但在 firebug 中我可以看到 X-Requested-With 设置为 XMLHttpRequest - 但仍然 respond_with 返回 html 而不是 js。

我错过了什么?

最佳答案

您的 Controller 应如下所示:

def new
@customer = Customer.new
respond_to do |format|
format.html # just renders like it normally would
format.js do
# Do whatever you need to do.
# By default it would render /customer/new.js.erb.
#
# If you're rendering some html from a view here, you'll want to
# tell rails not to render the layout by saying: render :layout => false
end
end
end

关于jquery - Rails 3 respond_to 不响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6697853/

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