我的rails应用程序正在使用API,我自动提交表单并重定向到另一个网站。我这样做如下:
<%= form_tag "https://www.<somewebsite>.action", method: :post,:protocol => 'https',:id => 'test' do %>
<%= hidden_field_tag 'field1', 'value1' %>
<%= hidden_field_tag 'field2', 'value2' %>
<%= hidden_field_tag 'field3', 'value3' %>
<% end %>
<script>document.getElementById('test').submit();</script>
这可以在另一个窗口中打开另一个网站。是否有可能在“iframe”中调用此重定向,以便屏蔽其他网站的URL。我不希望用户知道他/她被重定向到另一个网站。
答案 0 :(得分:0)
是的,这绝对是可能的,并且对用户来说是不可见的。我的公司有这样做的客户,所以他们可以无形地将我们的代码嵌入他们的网站。
您可以在其中创建一个var xhr = new XMLHttpRequest();
xhr.open("get", "file1.js", true);
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
var script = document.createElement("script");
script.type = "text/javascript";
script.text = xhr.responseText;
document.body.appendChild(script);
//Adding to head
//document.getElementsByTagName("head")[0].appendChild(script);
}
}
};
xhr.send(null);
的主页面。加载主页面后,iframe
会指向您自己的页面,其中只包含上述代码中的表单:
iframe
相应地调整<iframe src="your_page_with_the_form.html" name="my_iframe" width="300" height="1200" frameborder="0" scrolling="auto" style="background-color:#F7F3DE;"></iframe>
iframe
和height
。然后,您可以创建your_page_with_the_form.html并将width
代码放入其中。
如果您的代码导致提交“弹出”form_tag
,那么您可能需要在iframe
中指定:target => 'my_iframe'
。
另一种方法是将表单保留在主页面上,并使用Javascript将表单数据发送到外部API。
答案 1 :(得分:0)
当且仅当form_tag
未正确设置X-Frame-Options
标头时才有可能。