[英]How to use the jQuery AJAX request and the Ruby on Rails 'render' method togheter?
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. 我正在使用Ruby on Rails 3.0.7和jQuery 1.6.1,我想知道如何在AJAX请求之后处理响应
xhr
, status
和ex
变量。
The AJAX request is: AJAX请求是:
$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? 应该如何在控制器中声明
render
方法来响应AJAX请求,以便能够在AJAX请求的error
或success
部分处理响应值( xhr
, status
和ex
变量)? 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. 在少数世界中,我想做的是(在控制器中)用一些数据响应AJAX请求,然后(另一方面)处理这些数据。
See 看到
How do you handle Rail's flash with Ajax requests?
你如何使用Ajax请求处理Rail的flash?
and 和
as well for details. 以及细节。 Here's an excellent article on the topic of rails, the Rails jQuery UJS driver, jQuery, and rails.js
这是关于rails,Rails jQuery UJS驱动程序,jQuery和rails.js主题的优秀文章
- It finds remote links, forms, and inputs, and overrides their click events to submit to the server via AJAX.
它找到远程链接,表单和输入,并覆盖其单击事件以通过AJAX提交到服务器。
- It triggers six javascript events to which you can bind callbacks to work with and manipulate the AJAX response.
它触发六个javascript事件,您可以绑定回调以使用和操作AJAX响应。
And then the article walks through how to do all this using jQuery, UJS and rails. 然后本文将介绍如何使用jQuery,UJS和rails完成所有这些工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.