我正在使用 Ruby on Rails 3.0.7 和 jQuery 1.6.1,我想知道如何处理响应 xhr
、status
和 ex
AJAX 请求后的变量。
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
}
});
在我的 Controller 中:
respond_to do |format|
format.js {
render ... # here should be properly stated the Ruby on Rails 'render' method
:status => 200
}
end
应该如何在 Controller 中声明 render
方法来响应 AJAX 请求,以便可以处理响应值(xhr
,status
和 ex
变量)在 AJAX 请求的 error
或 success
部分?那么,我如何在检索到这些响应值后处理它们(例如,在警报消息中显示这些值)?
在少数情况下,我想做的是(在 Controller 中)用一些数据响应 AJAX 请求,然后(在另一端)处理这些数据。
最佳答案
见
和
以及详细信息。这是 an excellent article关于 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.
- It triggers six javascript events to which you can bind callbacks to work with and manipulate the AJAX response.
然后文章介绍了如何使用 jQuery、UJS 和 rails 完成所有这些工作。
关于jquery - 如何同时使用 jQuery AJAX 请求和 Ruby on Rails 'render' 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6451132/