gpt4 book ai didi

javascript - 为 标签发出的请求设置自定义 header

转载 作者:技术小花猫 更新时间:2023-10-29 12:09:32 24 4
gpt4 key购买 nike

假设我有这样的代码:

<img src='images/whatever.jpj' width='' height=''/>

如何为此请求配置自定义 header ?

将不胜感激。

最佳答案

我来晚了,但你可以使用 XMLHttpRequest 和一个 blob 来完成。

var xhr = new XMLHttpRequest();
xhr.responseType = 'blob'; //so you can access the response like a normal URL
xhr.onreadystatechange = function () {
if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
var img = document.createElement('img');
img.src = URL.createObjectURL(xhr.response); //create <img> with src set to the blob
document.body.appendChild(img);
}
};
xhr.open('GET', 'http://images.example.com/my_secure_image.png', true);
xhr.setRequestHeader('SecretPassword', 'password123');
xhr.send();

如果需要,您可以检查以确保 blob 的 MIME 类型是图像。

关于javascript - 为 <img/> 标签发出的请求设置自定义 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27000152/

24 4 0