简体   繁体   English

使用Javascript操作响应format.html

[英]Respond to format.html with Javascript action

I'd like to execute some javascript on the respond_to action for format.html. 我想对format.html的response_to操作执行一些javascript。

window.top.location.href='#{ @recurring_charge.confirmation_url }'

I need to execute javascript to open the window out of the iframe, otherwise I cannot render the page due to the following error message 'X-Frame-Options'to 'deny'. 我需要执行javascript才能从iframe中打开窗口,否则由于以下错误消息“ X-Frame-Options”为“ deny”,因此无法渲染页面。

How can I do this? 我怎样才能做到这一点?

  def confirm_billing
    current_shop.with_shopify!
    @recurring_charge = RecurringCharge.new(recurring_charge_params)
    if @recurring_charge.save
      respond_to do |format|
        format.html { render :js => "window.top.location.href='#{ @recurring_charge.confirmation_url }'" }
        format.json { render json: { success: true, redirect_url: @recurring_charge.confirmation_url }, status: 201 }
      end
    else
      respond_to do |format|
        format.html { redirect_to root_url, notice: 'Something went wrong' }
        format.json { render json: { success: false, errors: @recurring_charge.errors.full_messages }, status: 201 }
      end
    end
  end

我能够使用render:text使它正常工作,简直不敢相信它确实有效!

format.html { render :text => "<script>window.top.location.href='#{ @recurring_charge.confirmation_url }';</script>" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM