Tag: ajax

保护AJAX API以防止其他网站利用它

我们目前正在开发一个使用简单JSON API(RoR)填充页面的站点。 数据对客户公开,但对我们的服务至关重要,我们希望确保我们的竞争对手不会泄露我们的数据。 您使用哪些方法来validation对API的AJAX请求是否合法?

使用Remotipart的Rails AJAX上传表单

我一直在试图让AJAX上传表单正常工作。 我正在使用Rails 3.2。 我在我的Gemfile中放了gem “remotipart”, “~> 1.0” ,运行bundle install并成功安装。 我以前有这个非ajax上传表单,我添加了:remote => true ,根据github页面上的文献: true, :remote => true %> Input File Import Data 这在我的data_imports_controller create动作中: def create file = params[:file] filename = file.original_filename end 我在jquery.remotipart.js页面上添加了一个javascript包含,因为它感觉很重要,尽管没有明确的指示这样做。 我尝试了它,它没有给出服务器错误: Completed 500 Internal Server Error in 4ms NoMethodError (undefined method `original_filename’ for nil:NilClass): app/controllers/data_imports_controller.rb:16:in `create’ 很明显我做了一些根本错误的事情,但我需要一只手。

在rails中提供静态JSON目标文件

如何从rails中的文件提供静态JSON对象? (我想在ajax调用中访问它)? 什么是最好的方法?

使用Rails 3 link_to进行ajax回调的正确方法是什么

这是场景。 简介:我有一个解决方案,但我想知道这是否是在Rails 3中使用AJAX回调的最佳方法。 问题:我有一个链接,当点击它应该使用AJAX更新页面上现有的html元素(在我的情况下是div ): Change Goes here 我的解决方案在我的projects/show.html.erb 是的}%> 我的TasksController如下: class TasksController < ApplicationController def index respond_to do |format| format.html format.js { render action: "index", script: true } end end end 在我的tasks/index.js.erb我有 $(“#project_content”)。html(“’tasks’)%>”); 问题:这一切都有效。 但为什么我必须做所有这些? 不存在(或者不存在)解决方案,我们所有人都习惯于这样做 true, :update=>”project_content”} %> 然后,这会将tasks/index.js.erb加载到密钥:update引用的任何元素中?

Rails 5.1.2 – form_with未在html中显示data-remote =“true”

Rails 5.1.2: 我正在尝试使用form_with根据Rails文档和此GitHub线程创建一个AJAX表单。 这段代码: 实际上这段代码: 都产生这个HTML: 为什么HTML中没有出现data-remote=”true” ,因为我发布的第一个链接表明它应该,以及如何让它出现?

Rails:通过Ajax传递Params

我需要通过javascript将params传递回服务器。 目前,我将它们传递给javascript,如下所示: sendParams(“”); 然后像这样发送回来: function sendParams(q){ $.ajax({ url: ‘/mymodel/myaction’, type: ‘post’, data: {‘q’:q}, contentType: ‘json’ }); } 在我的控制器中,我试着像使用任何其他参数一样使用它们: MyModel.where(params[:q]) 但即使firebug在POST标签中显示了这个内容,params仍然是空的: q=%7B%26quot%3Bc%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Ba%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bname%26quot%3B%3A%26quot%3Btitle%26quot%3B%7D%7D%2C%26quot%3Bp%26quot%3B%3A%26quot%3Bcont%26quot%3B%2C%26quot%3Bv%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bvalue%26quot%3B%3A%26quot%3B2%26quot%3B%7D%7D%7D%7D%2C%26quot%3Bs%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bname%26quot%3B%3A%26quot%3Bvotes_popularity%26quot%3B%2C%26quot%3Bdir%26quot%3B%3A%26quot%3Bdesc%26quot%3B%7D%7D%7D 知道为什么这些信息没有被where子句处理? 我该怎么做才能使params Rails再次可读? 更新: Started POST “/publications/search?scroll=active&page=6” for 127.0.0.1 at 2013-0 2-12 22:55:24 -0600 Processing by PublicationsController#index as */* Parameters: {“scroll”=>”active”, “page”=>”6”} 更新2: 问题显然源于contentType 。 当我删除它,然后q作为Rails参数发送。 不幸的是, q仍然是JSON,导致错误: undefined method `with_indifferent_access’ for # 如何将JSON转换为params哈希?

如何使用rails remote:true参数和JSON?

在使用rails remote:true参数提交ajax请求时,如何指定要返回JSON对象? 一旦返回JSON对象,我如何捕获并使用它?

Rails,Backbone,PhoneGap,CORS(Access-Control-Allow-Origin错误不允许)

我正在构建一个使用backbone.js和Rails后端的Phonegap应用程序。 在创建新用户时,我收到了与CORS相关的错误: 我在http://0.0.0.0:8000 python -m SimpleHTTPServer)上运行我的PhoneGap网络应用程序,并在webrick上运行我的Rails应用程序在http://0.0.0.0:3000 rails服务器)。 尝试在Backbone中创建一个新的“Spot”时会出现问题(chrome js控制台): > s = new App.Models.Spot() (creates Spot) > s.save() (returns error Object) OPTIONS http://0.0.0.0:3000/spots.json 404 (Not Found) jquery-1.8.2.js:8416 XMLHttpRequest cannot load http://0.0.0.0:3000/spots.json. Origin http://0.0.0.0:8000 is not allowed by Access-Control-Allow-Origin. 这是我的应用程序控制器: def set_access_control_headers headers[‘Access-Control-Allow-Origin’] = ‘http://0.0.0.0:8000’ headers[‘Access-Control-Request-Method’] = ‘POST, GET’ end 我已经阅读了很多文章,我可以得到的最远的是修改我的routes.rb以包含这个: match ‘*all’ => ‘application#cor’, :constraints […]

使用Ajax进行Omniauth授权调用

当我使用它作为链接时,我的Rails 3应用程序中的omniauth正常工作: link_to(“Connect Twitter”, “/auth/twitter”, :id => “connect-to-twitter”) 现在我想通过ajax调用’/ auth / twitter’。 但是,在请求阶段之后不会返回任何内容。 这是最后一个日志条目: Started GET “/auth/twitter” for 127.0.0.1 at 2012-10-30 17:53:02 -0700 (twitter) Request phase initiated. 这是我的ajax代码(在Coffeescript中): $(“#connect-to-twitter”).live(“click”, -> alert(‘get twitter’) $.get(‘/auth/twitter’, (data) -> alert(data) ) return false ) (我知道这个问题在此之前已被问过,但我没有看到任何真正的答案。)

为什么Safari会导致Rails CSRFexception?Chrome不是?

我想通过iframe中的AJAX请求在我的Rails 4应用程序中创建会话。 在iframe中,我通常使用属性remote: true包含一个新会话的表单,并在表单正文中包含以及在布局的头部包含 。 Chrome发布此表单并创建会话没有问题。 在相同条件下,Safari会导致CSRFexception。 为什么会发生这种情况,我该怎么做才能阻止它呢? 据我了解,这不是CSRF必不可少的情况,因为没有会话劫持,但我仍然担心要关闭它。 Chrome版本:31.0.1650.63 Safari版本:7.0.1