简体   繁体   English

如何使用jQuery AJAX请求和Ruby on Rails'render'方法来实现?

[英]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请求之后处理响应xhrstatusex变量。

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请求的errorsuccess部分处理响应值( xhrstatusex变量)? 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

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主题的优秀文章

  1. It finds remote links, forms, and inputs, and overrides their click events to submit to the server via AJAX. 它找到远程链接,表单和输入,并覆盖其单击事件以通过AJAX提交到服务器。
  2. 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.

相关问题 数据库,Jquery,Ajax和Zend框架。 如何使用所有togheter? - Database, Jquery, Ajax and Zend Framework. How to use all togheter? 如何使用Javascript和AJAX渲染索引页上的弹出窗口Ruby on Rails 5 - How to use Javascript and AJAX to render popup on index page Ruby on Rails 5 关于Rails的Ajax如何使用Ajax append方法来呈现HTML? - Ajax on Rails How to use Ajax append method for render HTML? 如何使用rails 3对jQuery ajax成功方法进行部分渲染 - How to do render partial on jQuery ajax success method with rails 3 Ruby on Rails - 发出 AJAX 请求并呈现结果模型的一部分 - Ruby on Rails - Make an AJAX request and render the partial of the resulting model 如何在 Rails 上的 Ruby 中将 jquery.click() 方法与 form.select 一起使用? - How to use jquery .click() method with form.select in Ruby on Rails? Ruby on Rails-执行Ajax以在JQuery单击上呈现部分内容 - Ruby on Rails - Execute ajax to render partial on JQuery click Ruby on Rails-通过jQuery / Ajax在另一个视图中渲染局部 - Ruby on Rails - Render a partial in another View via jQuery/Ajax 如何使用Rails渲染与Ajax JavaScript请求 - How to work with rails render with ajax javascript request 如何在Rails AJAX请求中使用Quicksand jQuery插件? - How do I use the Quicksand jQuery plugin with a Rails AJAX request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM