这是我的ajax处理代码:
$.ajax({
url: "shirts/first?page=" + params[:page],
type: "GET"
})
如何告诉 Ajax 从衬衫/第一个文件的下一页获取结果? 我按照我所展示的方式编写了代码,但它抛出一个错误,提示“语法错误”!我该如何解决这个问题?
还有我的 .js.erb 文件(如果有帮助的话):
$("#container1").append("<%= escape_javascript(render 'shirts/first')%>");
最佳答案
如果您要执行 Ajax 分页,则必须确保可以在 Controller 上处理 Ajax 请求(使用 respond_to
),并发送正确的数据:
JS
#app/assets/javascripts/application.js
$("a.pages").on("click", function() {
$.ajax({
url: "shirts/first?page=" + $(this).attr("id"),
type: "GET"
});
});
为此,您需要使用带有页面 ID 的分页按钮
<小时/>Controller
#app/controllers/shirts_controller.rb
def first
@shirts = Shirt.where(your_query)
respond_to do |format|
format.html
format.js
end
end
<小时/>
查看
#app/views/shirts/first.js.erb
$("#container1").append("<%= raw(escape_javascript(render 'shirts/first')) %>");
关于javascript - 在 Ajax 中从另一个页面选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21550469/