无法从 form_tag 发送 ajax 请求
我试图从表单标签发送 ajax 请求,但没有发送 XHR 请求。如果我从链接中执行相同操作,一切都会完美运行。
这是我可以从链接或表单发送请求的视图:
<div id="request_test">
<%= link_to "test", explorer_test_path(:format => :js), :remote => true %>
<%= form_tag explorer_test_path(:format => :js), :id => "testform", :remote => true, :method => :get do %>
<%= submit_tag "Send test request"%>
<% end %>
</div>
<div id="response_test"></div>
然后在我的控制器中检查它是否是 XHR 请求,然后呈现响应 ajax 请求的 javascript:
def test
if request.xhr?
logger.debug "xhr"
else
logger.debug "not xhr"
end
respond_to do |format|
format.js
end
end
我的 test.js.erb 是非常简单:
$("#response_test").html("<b>hi</b>");
结果是,如果我单击链接,我会得到我正在寻找的行为,即response_test div 读取“hi”。如果我提交表单,我只需将 test.js.erb 中的 javascript 作为字符串写出,服务器日志告诉我该表单未提交 XHR 请求。如果我在 firebug 中调试整个内容,我会得到相同的结果,链接发送 ajax 请求,而提交的表单则不会。在这两种情况下,我的控制台都告诉我服务器正在将请求作为 JS 处理,因此不应该有任何数据类型问题。
使用 jquery UJS 运行 Rails 3.0.4。
感谢任何关于问题可能是什么的提示,我已经尝试解决这个问题有一段时间了,但由于我对 Rails 相对较新,我怀疑我错过了一些基本的东西。
更新:从 (http://www.alfajango. com/blog/rails-jquery-ujs-now-interactive/) 修复了该问题,这表明 :remote =>; jquery ujs 未找到或正确解释我的表单标记上的 true。我认为这是一种解决方法,而不是永久修复,因此如果有人知道我的原始代码中有什么问题,我将非常感激。
$('#testform').live('submit', function(e) {
$.rails.handleRemote( $(this) );
e.preventDefault();
});
I am trying to send a ajax request from a form tag, but no XHR request is sent. If I do the same from a link everything works perfectly.
Here's the view from which I can send the request either from a link or a form:
<div id="request_test">
<%= link_to "test", explorer_test_path(:format => :js), :remote => true %>
<%= form_tag explorer_test_path(:format => :js), :id => "testform", :remote => true, :method => :get do %>
<%= submit_tag "Send test request"%>
<% end %>
</div>
<div id="response_test"></div>
In my controller I then check if it is a XHR request or not and then render the javascript responding to the ajax request:
def test
if request.xhr?
logger.debug "xhr"
else
logger.debug "not xhr"
end
respond_to do |format|
format.js
end
end
My test.js.erb is very simple:
$("#response_test").html("<b>hi</b>");
The result is that if I click the link I get the behavior I am looking for, ie that the response_test div reads "hi". If I submit the form I simply get the javascript in test.js.erb written out as a string and the server log telling me that the form didn't submit a XHR request. If i debug the whole thing in firebug I get the same result, the link sends an ajax request, the submitted form does not. In both cases my console tells me that the server is processing the request as JS, so it shouldn't be any data-type problem.
Running Rails 3.0.4 with jquery UJS.
Thankful for any hints to what the problem might be, I have tried to figure this out for quite some time now but since I am relatively new to Rails I suspect that I am missing something basic.
UPDATE: Adding the following javascript from (http://www.alfajango.com/blog/rails-jquery-ujs-now-interactive/) fixes the problem, which suggests that the :remote => true on my form tag isn't found or interpreted correctly by the jquery ujs. I see this as a workaround more than a permanent fix so if anybody have any idea of what is wrong in my original code I would be very grateful.
$('#testform').live('submit', function(e) {
$.rails.handleRemote( $(this) );
e.preventDefault();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论