更改 iframe src 时如何设置自定义 http 标头? [英] How to set custom http headers when changing iframe src?
本文介绍了更改 iframe src 时如何设置自定义 http 标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在使用 javascript 更改源 (src) 时,是否可以将自定义 http 标头添加到 <iframe>
完成的请求中?
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.
也许试试这个变体.
$.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屋!
查看全文