如何使用jQuery的AJAX请求和Ruby on Rails的“渲染”的方法togheter? [英] How to use the jQuery AJAX request and the Ruby on Rails 'render' method togheter?
问题描述
我在使用Ruby on Rails 3.0.7和jQuery 1.6.1,我想知道如何处理响应 XHR
,状态
AJAX请求后前
变量。
Ajax请求是:
$ jQuery.ajax({
键入:POST,
网址:< request_to_the_controller_method>中,//请参见下面的code
错误:函数(XHR,状态,前){
//处理与XHR','地位'和'前'变量
}
成功:函数(jqXHR,状态,前){
//处理与XHR','地位'和'前'变量
}
});
在控制器我有:
的respond_to办|格式|
format.js {
使...#这里应适当规定的Ruby on Rails的渲染的方法
:状态=> 200
}
结束
如何应在控制器中的渲染
方法来响应AJAX请求,这样就可以处理响应值来表示( XHR
,状态
和前
变量)在错误
或成功
AJAX请求的部分?那么,我该怎么处理响应值后,我取出那些(例如,以显示一条警告消息,这些价值)?
在一些世界,我想要做的是(控制器),以一个Ajax请求一些数据,然后(在另一侧)响应处理此数据。
请参阅
<一个href="http://stackoverflow.com/questions/366311/how-do-you-handle-rails-flash-with-ajax-requests">How你怎样对待铁路的闪光灯Ajax请求?
和
以及了解详细信息。这里是的优秀文章在轨道上的话题, Rails的jQuery的UJS驱动程序,jQuery和rails.js
- 在它找到远程链接,表单和输入,并覆盖它们的点击事件,通过AJAX提交给服务器。
- 在它触发6 JavaScript的事件,你可以绑定回调使用和处理Ajax响应。
然后本文走过如何做到这一切使用jQuery,UJS和护栏。
I am using Ruby on Rails 3.0.7 and jQuery 1.6.1 and I would like to know how to handle response xhr
, status
and ex
variables after an AJAX request.
The AJAX request is:
$jQuery.ajax({
type: "POST",
url: "<request_to_the_controller_method>", // see the code below
error: function(xhr, status, ex) {
// handling with 'xhr', 'status' and 'ex' variables
}
success: function(jqXHR, status, ex) {
// handling with 'xhr', 'status' and 'ex' variables
}
});
In the controller I have:
respond_to do |format|
format.js {
render ... # here should be properly stated the Ruby on Rails 'render' method
:status => 200
}
end
How SHOULD be stated in the controller the render
method to respond to the AJAX request so that it is possible to handle response values (xhr
, status
and ex
variables) in the error
or success
section of the AJAX request? Then, how can I handle the response values after I retrieved those (for example to show those values in an alert message)?
In few worlds, what I would like to do is (in the controller) to respond to an AJAX request with some data and then (on the other side) handle this data.
See
and
as well for details. Here's an excellent article on the topic of rails, the Rails jQuery UJS driver, jQuery, and rails.js
- It finds remote links, forms, and inputs, and overrides their click events to submit to the server via AJAX.
- It triggers six javascript events to which you can bind callbacks to work with and manipulate the AJAX response.
And then the article walks through how to do all this using jQuery, UJS and rails.
这篇关于如何使用jQuery的AJAX请求和Ruby on Rails的“渲染”的方法togheter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!