Rails InvalidCrossOriginRequest
我在我的Rails应用程序页面上有一个remote: true
链接,它调用同一页面的.js
版本,然后运行脚本来更新页面内容。
它工作正常,但从昨天起我现在得到Security warning: an embedded tag on another site requested protected JavaScript. If you know what you're doing, go ahead and disable forgery protection on this action to permit cross-origin JavaScript embedding.
Security warning: an embedded tag on another site requested protected JavaScript. If you know what you're doing, go ahead and disable forgery protection on this action to permit cross-origin JavaScript embedding.
每次我点击其中一个链接。
关于如何阻止这一点的任何想法?
将其添加到呈现页面片段的控制器
class FooController < ApplicationController protect_from_forgery except: :index
其中index
是您希望跳过此保护的操作的名称
跨域政策
您得到的错误是因为所谓的跨源策略。 它是每个浏览器中存在的标准,它不允许页面从其他域运行脚本。 解决此问题的方法是在http请求中添加标头,以允许您的内容在其他域中运行。
维基百科对此有一个解释:
用于放宽同源策略的第二种技术是以跨源资源共享的名称标准化的。 此标准使用新的Origin请求标头和新的Access-Control-Allow-Origin响应标头扩展HTTP。 它允许服务器使用标头显式列出可能请求文件或使用通配符的源,并允许任何站点请求文件。 诸如Firefox 3.5,Safari 4和Internet Explorer 10之类的浏览器使用此标头来允许具有XMLHttpRequest的跨源HTTP请求,否则这些请求将被同源策略禁止。