如何在改变iframe src时设置自定义http头?

您所在的位置:网站首页 sdwebimage源码 如何在改变iframe src时设置自定义http头?

如何在改变iframe src时设置自定义http头?

2023-04-20 21:43| 来源: 网络整理| 查看: 265

百度翻译此文   有道翻译此文 问题描述

Is there a way to add a custom http header into the request done by an when changing the source (src) using javascript?

推荐答案

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)) } });

This is assuming the iframe is pointing at a cross domain src. It is simpler if everything is on the same domain.

Edit: 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); } }); 其他推荐答案

Rather than using a data URI, or setting the contents to a string, you can use URL.createObjectURL(), and set it as the src of the iframe.

var xhr = new XMLHttpRequest(); xhr.open('GET', 'some.pdf'); xhr.onreadystatechange = handler; xhr.responseType = 'blob'; xhr.setRequestHeader('Authorization', 'Bearer ' + token); xhr.send(); function handler() { if (this.readyState === this.DONE) { if (this.status === 200) { // this.response is a Blob, because we set responseType above var data_url = URL.createObjectURL(this.response); document.querySelector('#output-frame-id').src = data_url; } else { console.error('no pdf :('); } } }

The object URLs are pretty interesting. They're of the form blob:https://your.domain/1e8def13-3817-4eab-ad8a-160923995170. You can actually open them in a new tab and see the response, and they're discarded when the context that created them is closed.

Here's a full example: https://github.com/courajs/pdf-poc

其他推荐答案

I ended up going with the approach proposed by the other answers here, that use ajax to get the html string and then directly set the contents of the iFrame.

However, I used the approach posted in this answer to actually set the contents of the iFrame, as I found it worked well cross platform with almost all devices I could dig up.

Tested - successful:

Chrome 54 (desktop) ^ Firefox 49 (desktop) ^ IE 11 (desktop) ^ IE 10 (desktop) in emulation mode ^ Safari/Chrome on iOS 8 (ipad) Chrome on Android 6 (nexus phone) Edge on Lumia 950 (Win 10 Phone)

^ confirmed that linked css and js in the content run correctly (others not tested)

Tested - unsuccessful:

IE 9 (desktop) in emulation mode Safari/Chrome on iOS 7 (iPhone)

So putting them together gives something like this (Note: I havn't actually run this exact code):

$.ajax({ type: "GET", url: "https://yourdomain.com/gethtml", beforeSend: function(xhr) { xhr.setRequestHeader("yourheader", "value"); }, success: function(data) { var iframeDoc = document.querySelector('#myiframe').contentWindow.document; iframeDoc.open('text/html', 'replace'); iframeDoc.write(data); iframeDoc.close(); } });

Here's an example of setting the iFrame contents in this JS Bin

Edit: Here's the html part

Edit 2:

The solution above appears to no longer be working in Firefox (50.1.0) for some unknown reason. Using the solution in this answer I've now changed to code to the example below, which also seems to be more robust:

$.ajax({ type: "GET", url: "https://yourdomain.com/gethtml", beforeSend: function(xhr) { xhr.setRequestHeader("yourheader", "value"); }, success: function(data) { var iframe = document.getElementById('myiframe'); iframe.contentWindow.contents = data; iframe.src = 'javascript:window["contents"]'; } });


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3