在 Rails 中处理 ajax 请求
Dealing with ajax request in Rails
Stackoverflow community我有一个选择。发送我的 ajax 请求的更改。
<%= f.select :id, options_from_collection_for_select(@rtypes, "id", "typeName"),
{include_blank: true },
{'data-rtypes': @rtypes.to_json } %>
.我正在使用Jquery ajax。我的 ajax 工作。它将 rtype 的 id 发送到 show_sub_types 方法。
$(function () {
// specify id or class for your select tag
$('select').on('change', function () {
var rtype = $(this).val();
$.ajax({
url: "/RequestTypes/show_sub_types/"+rtype,
type: "GET",
})
});
});
在我的show_sub_types方法中,我想从 RequestSubType 模型中获取所有子类型(类型)。
def show_sub_types
@rtype = params[:id];
@stypes = RequestSubType.where("RequestType_id"==@rtype).all
respond_to do |format|
... some code here
end
end
我不知道如何处理 ajax 请求,我不知道如何将我的 stypes 数组发送到页面,以及如何处理该响应。我已经阅读了一些教程,但仍然无法理解respond_to部分。也许我会以我自己的例子来理解。在我看来,我有div 我想放置 ajax 发送的数据(插入到 html 中)。
指定select
的id
并读取data
属性。 在数据变量中获取该数组,并将其传递给 ajax 请求
var data = $(this).data('rtypes');
or
$(this).find(':selected').data('rtypes')
$.ajax({
type : "POST",
url : "/RequestTypes/show_sub_types/"+rtype,
dataType: 'json',
data: JSON.stringify(data),
dataType: "json",
contentType: 'application/json'
});
相关文章:
- ajax请求的顺序总是不同的
- 从ajax请求中获取javascript对象
- Ajax请求文档就绪会导致jquery加载缓慢
- MockJax没有在JavaScript应用程序中发送对我AJAX请求的响应
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- JavaScript代码未正确检查ajax请求
- "日期“;AJAX请求返回的类型值未定义
- 在等待ajax请求时显示微调器并禁用页面
- 跨域ajax请求
- Ajax请求返回空的数据字符串,但首先得到了正确的数据
- Javascript-如何让脚本与Ajax请求的数据一起运行
- ajax请求成功,但可以'我看不到我的数据
- 如何将给定的curl命令复制为jquery ajax请求
- 为什么我能够从javascript控制台发送跨域ajax请求
- 一台特定计算机的Ajax请求数据未定义/失败
- 获取ajax请求的请求头
- 原型Ajax请求参数为嵌套json
- 可以't从AJAX请求中筛选数据
- jQuery Mobile Collapse上的AJAX请求
- 暂停函数执行流程,直到ajax请求完成