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? Then, how can I handle the response values after I retrieved those (for example to show those values in an alert message)?
應該如何在控制器中聲明render方法來響應AJAX請求,以便能夠在AJAX請求的錯誤或成功部分處理響應值(xhr,status和ex變量)?那么,在檢索到響應值之后如何處理響應值(例如在警報消息中顯示這些值)?
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請求,然后(另一方面)處理這些數據。
3
See
看到
How do you handle Rail's flash with Ajax requests?
你如何使用Ajax請求處理Rail的flash?
and
和
Error Handling with Ajax in Rails 3
在Rails中使用Ajax進行錯誤處理3
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完成所有這些工作。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/06/23/fa60ff19c20f9248f9e9c4070d66ffb3.html。