admin管理员组文章数量:1315326
Hi Unsplash allows to load random image from their website via:
if I access the url from the browser each time the url generates random static image eg:
.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=2560&h=1440&fit=crop&s=a7f66c8417bcf79d2503b84db64c7b1a
I would like to request the image in jquery or js via the first url and in response get the second one. Is this possible?
Hi Unsplash allows to load random image from their website via:
https://source.unsplash./random/2560x1440
if I access the url from the browser each time the url generates random static image eg:
https://images.unsplash./photo-1488616268114-d949a6d72edf?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=2560&h=1440&fit=crop&s=a7f66c8417bcf79d2503b84db64c7b1a
I would like to request the image in jquery or js via the first url and in response get the second one. Is this possible?
Share Improve this question asked May 14, 2017 at 5:48 BenBen 3452 gold badges4 silver badges16 bronze badges 1- I'm having similar problem with laravel. Please help me. stackoverflow./questions/59167996/…? – Mael Commented Dec 4, 2019 at 6:47
4 Answers
Reset to default 2You can use the responseURL of the XMLHttpRequest.
MDN Reference
and this answer for an example in jQuery and native JS.
This is tricky since there are several things going on.
If you use Chrome or Firefox, open the developer tools and review the network tab you will see that the original request returns an HTTP 302 redirect to a different URL:
The browser then follows the specified Location
header and you get the page that you see. The image is within an <img>
on that page.
So to get the final image you need to:
- Do an ajax request to the initial URL
- Parse the response headers to get the new location
- Do an ajax request to the new location
- Parse the HTML in the new response to grab the actual image URL out of the
img
tag.
Good luck!
You can use PerformanceObserver
to get the name
property value of the requested resource
const key = "unsplash";
const url = `https://source.${key}./random/2560x1440`;
let bloburl = void 0;
let img = new Image;
const getResourceName = () => {
let resource = "";
const observer = new PerformanceObserver((list, obj) => {
// alternatively iterate all entries, check `"name"`
// property value for `key`, break loop if specific resource requested
for (let entry of list.getEntries()) {
let {name: res} = entry.toJSON();
resource = res;
}
});
observer.observe({
entryTypes: ["resource"]
});
return fetch(url)
.then(response => response.blob())
.then(blob => {
observer.disconnect();
bloburl = URL.createObjectURL(blob);
img.src = bloburl;
document.body.appendChild(img);
return resource
})
}
getResourceName().then(res => console.log(res)).catch(err => console.log(err))
You can alternatively use Response.url
const key = "unsplash";
const url = `https://source.${key}./random/2560x1440`;
let bloburl = void 0;
let img = new Image;
const getResourceName = fetch(url)
.then(response => Promise.all([response.url, response.blob()]))
.then(([resource, blob]) => {
bloburl = URL.createObjectURL(blob);
img.src = bloburl;
document.body.appendChild(img);
return resource
});
getResourceName.then(res => console.log(res)).catch(err => console.log(err))
fetch('https://source.unsplash./random').then(res=>{console.log(res)})
from source worked for me.
本文标签: javascriptGet actual image url after redirectStack Overflow
版权声明:本文标题:javascript - Get actual image url after redirect - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741976309a2408145.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论