如何在更改iframe src时设置自定义http标头? [英] How to set custom http headers when changing iframe src?
本文介绍了如何在更改iframe src时设置自定义http标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用javascript更改源代码(src)时,有没有办法在< iframe>
的请求中添加自定义http标头?
Is there a way to add a custom http header into the request done by an <iframe>
when changing the source (src) using javascript?
推荐答案
您可以将ajax请求的结果设置为iframe的内容,如下所示:
You can have the results of an ajax request that has custom headers be set as the content of an iframe like so:
$.ajax({
type: "GET",
url: "https://app.icontact.com/icp/a/",
contentType: "application/json",
beforeSend: function(xhr, settings){
xhr.setRequestHeader("some_custom_header", "foo");},
success: function(data){
$("#output_iframe_id").attr('src',"data:text/html;charset=utf-8," + escape(data))
}
});
这是假设iframe指向跨域src。如果所有内容都在同一个域中,则会更简单。
This is assuming the iframe is pointing at a cross domain src. It is simpler if everything is on the same domain.
编辑:也许尝试这种变化。
Maybe try this variation.
$.ajax({
type: "GET",
url: "https://app.icontact.com/icp/a/",
contentType: "application/json",
beforeSend: function(xhr, settings){
xhr.setRequestHeader("some_custom_header", "foo");},
success: function(data){
$("#output_iframe_id").attr('src',"/")
$("#output_iframe_id").contents().find('html').html(data);
}
});
这篇关于如何在更改iframe src时设置自定义http标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文