如何使用导轨遥控:使用JSON真正的参数?导轨、如何使用、参数、JSON

2023-09-10 17:19:03 作者:曲未完、词已尽

我如何指定我想返回一个JSON对象提交与轨道遥控一个Ajax请求时:真正的参数?而一旦JSON对象返回我该如何抓住并用它?

How do I specify that I want a JSON object to be returned when submitting an ajax request with the rails remote: true parameter? And once the JSON object is returned how do I catch and use it?

推荐答案

有很多方法来回答这个取决于你的具体情况(如:你是使用jQuery和UJS适配器),但我要发布的东西从我用我自己,以便更好地了解整个过程的文档: 我用这个大帖子到很大程度的:我会更新我的答案,因为我目前通过这个东西自己工作压力太大

There are many ways to answer this depending on your specifics (eg: you are using jquery and the UJS adapter) but I'm going to post stuff from a doc I'm using myself to better understand the whole process: I use this Great post to a large degree: I'll update my answer as I am currently working through this stuff myself too.

您可以添加数据类型的属性,像这样的DOM元素:

You can add the data-type attribute to the DOM element like this:

<%= link_to "Add a new task", new_project_task_path(@project), "data-type" => "json", :id => "add_task_btn" %>

您还可以定义jQuery的全球$ .ajaxSetup()函数的默认数据类型。改变所有的Ajax请求:

You can also define a default dataType in jQuery's global $.ajaxSetup() function. Changes all Ajax requests:

$.ajaxSetup({
      dataType: 'json'
});

和第三种方式是在beforeSend回调:

And the third way is in the beforeSend callback:

$("#add_some_button").live("ajax:beforeSend", function(e, xhr, settings){
        new_data_type = "application/json, text/javascript, */*; q=0.01";
        xhr.setRequestHeader('accept', new_data_type);
})

(第q = 0.01是介于0和preference 1)

(the q=0.01 is the preference between 0 and 1)

所以,你可以改变数据类型属性和HTTP头,以满足您的需求。 仅供参考以下是可能的MIME类型的列表:这里

So you can change that  dataType property and HTTP Headers to fit your needs. For reference here is a list of possible MIME types: Here

和特定的JavaScript的媒体类型列表:这里

And the specific javascript media type list: Here

数据格式的概述被发送回从服务器: 1。)的Ajax发送与接收报头设置为所需的MIME类型的请求。 2)Rails的控制器检查Accept头,以确定它应该返回。 3.)控制器将确定该动作是否处理特定MIME类型。 在Rails 3中我们可以使用respond_with,pre 3.0的一个respond_to代码块中的控制器动作。

Overview of data format being sent back from the server: 1.) Ajax sends the request with the Accept header set to the desired MIME type. 2.) Rails Controller checks the Accept header to determine what it should return. 3.) Controller will determine whether the action handles the particular MIME type. In rails 3 we can use respond_with, pre 3.0 its a respond_to block in the controller action.

阿贾克斯可以在Rails的3.1中使用的方法概述:

Overview of the ways Ajax can be used in Rails 3.1:

我认为,preferred方法现在是使用的缺省数据类型为脚本,然后处理Ajax请求与任何一个js.erb或js.coffee文件。

I believe that the preferred method now is to use the default dataType as script and then handle AJAX requests with either a js.erb or a js.coffee file.

选项: 1)单击事件:插入DOM元素 2)提交通过Ajax表单 3)通过Ajax删除 4)客户端验证与阿贾克斯(在输入栏上使用的数据远程) 5)添加动态选择(在选择字段使用数据远程) 6)阿贾克斯分页 - 返回HTML直接。

Options:  1.) Click Event: Inserting DOM Elements 2.) Submitting a Form via Ajax 3.) Deleting via Ajax 4.) Client-side Validation with Ajax (Using data-remote on an input field) 5.) Adding Dynamic Selects (Using data-remote on a select field) 6.) Ajax Pagination – Return HTML directly. 

再次什么我包括在这里是我自己的大纲/审查,以获得详细信息,请看看安德烈·辛格的伟大的职位的这里。

Again what I am including here is my own outline/review, to get details please check out Andrea Singh's great post here.