![](/img/trans.png)
[英]How to sort out this error “ A ActionController::InvalidCrossOriginRequest occurred in ”
[英]CSRF Error in Production: ActionController::InvalidCrossOriginRequest
我僅在生產環境中的注釋控制器上收到以下錯誤(在開發中工作正常)。 用戶流程如下:我有一個jQuery,當按下特定按鈕時該jQuery將運行,該按鈕呈現部分文件以添加新注釋(一種簡單形式)。 控制器中.js請求的response_to方法用於new.js.erb文件。 這應該相對容易做到,但是Rails(我正在使用Rails 4.1.1)代碼或我的服務器(Rackspace Cloud Server)上出現了問題。 錯誤如下:
CommentsController#new中的ActionController :: InvalidCrossOriginRequest安全警告:另一個網站上的嵌入式標記請求受保護的JavaScript。 如果您知道自己在做什么,請繼續對此操作禁用偽造保護,以允許跨域JavaScript嵌入。
我已經在我的注釋控制器中嘗試了以下代碼(不起作用)。 它只是在瀏覽器中將.js文件呈現為文本字符串(javascript不起作用)。
protect_from_forgery except: :new
skip_before_action :verify_authenticity_token
我嘗試用application_controller.rb文件中的:: exception方法刪除protect_from_forgery,但是它不起作用(只是將JavaScript在瀏覽器中呈現為文本字符串)。
我試過用“ protect_from_forgery with::null_session”替換“ protect:from :: exception”,但這也不起作用(給出與上面相同的InvalidCrossOriginRequest錯誤)。
我沒有辦法解決此問題。 同樣,它僅在生產中發生。 在我的本地計算機上(通過localhost),一切正常。 我的評論控制器的代碼如下:
class CommentsController < ApplicationController
# before_action :set_comment, only: [:show, :edit, :update, :destroy]
before_action :load_topic
before_action :authenticate_user!
# protect_from_forgery except: :new
# skip_before_action :verify_authenticity_token
# GET /comments
# GET /comments.json
def index
@comments = Comment.all
end
# GET /comments/1
# GET /comments/1.json
def show
end
# GET /comments/new
def new
@comment = Comment.new
end
# GET /comments/1/edit
def edit
end
# POST /comments
# POST /comments.json
def create
@comment = @topic.comments.new(comment_params)
@comment.user_id = current_user.id
respond_to do |format|
if @comment.save
format.html { redirect_to @topic, notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: @comment }
format.js
else
format.html { redirect_to @article, alert: 'Unable to add comment' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
format.js { render 'fail_create.js.erb'}
end
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
format.json { render :show, status: :ok, location: @comment }
else
format.html { render :edit }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
@comment = @topic.comments.find(params[:id])
@comment.destroy
respond_to do |format|
format.html { redirect_to @topic, notice: 'Comment was successfully deleted.' }
format.json { head :no_content }
format.js
end
end
private
def load_topic
@topic = Topic.find(params[:topic_id])
end
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:topic_id, :body, :name)
end
end
任何有關解決此問題的建議將不勝感激。
這是否真的發生在真實用戶上,還是您僅在日志/監控中看到此錯誤?
當抓取工具訪問您的網站時,通常會發生此錯誤(在您的開發環境中顯然不會發生)。
該文檔建議您將這些添加到控制器操作中:
skip_before_action :verify_authenticity_token, if: :json_request?
protected
def json_request?
request.format.json?
end
但是,如果不是這種情況,我認為您實際上有一個CORS問題。 2個可能的原因:
如果您編輯主機文件並將域指向本地服務器,則也可以嘗試在開發中重現此內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.