簡體   English   中英

使用GET重定向時的ActionController :: InvalidCrossOriginRequest

[英]ActionController::InvalidCrossOriginRequest on redirect with GET

我正在嘗試使用在模式內顯示的表單來構建索引頁面。 表單以JS的POST動作提交后,觸發#create動作,該動作也以JS的redirect_to動作#index響應

def create
  @output = Output.new(output_params)

  respond_to do |format|
    if @output.save
      format.html { redirect_to @output, notice: 'Output was successfully created.' }
      format.json { render :show, status: :created, location: @output }
      format.js { redirect_to production_line_path @output.machine.production_line, machine_id: @output.machine_id, format: :js }
    else
      format.html { render :new }
      format.json { render json: @output.errors, status: :unprocessable_entity }
    end
  end
end

我的問題是,這給以下說明帶來了錯誤

安全警告:另一個站點上的嵌入式<script>標記請求受保護的JavaScript。 如果您知道自己在做什么,請繼續對此操作禁用偽造保護,以允許跨域JavaScript嵌入。

在35毫秒內完成422無法處理的實體(查看:27.2毫秒| ActiveRecord:1.5毫秒)

ActionController :: InvalidCrossOriginRequest(安全警告:另一個站點上的嵌入式<script>標記請求受保護的JavaScript。如果您知道自己在做什么,請繼續對此操作禁用偽造保護以允許跨域JavaScript嵌入。):

我不確定我是否嘗試以錯誤的方式進行此操作。 我是否需要更改JS的響應才能不重定向,而是從同一控制器動作#create

您可以使用內置的redirect_to並為其傳遞路徑和參數,也可以使用JS進行重定向。 如果要重定向,請執行以下操作:

render js: "window.location.pathname = #{production_line_path @output.machine.production_line, machine_id: @output.machine_id}"

我想我發現了InvalidCrossOriginRequest錯誤的原因。 錯誤地,我的表單是作為不帶令牌的JS請求發送的,通常是由UJS Rails適配器自動添加的,但是如果有人使用它。

我的表單缺少remote: true ,而是包含format: :js ,這讓我覺得我正在使用UJS Rails適配器,並且我的POST請求帶有用於CSRF保護的必需令牌。 跨站請求偽造(CSRF)的更多內容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM