读取图像的绝对重定向SRC属性URL [英] Read the absolute redirected SRC attribute URL for an image

查看:575
本文介绍了读取图像的绝对重定向SRC属性URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Twitter头像API在客户端获取用户的头像URL,我想缓存响应,但不是常规JSON(或者实际上是任何其他类型),他们只是发出302重定向。

I'm using the Twitter avatar API to fetch a user's avatar URL on the client-side, and I'd like to cache the response, but instead of a regular JSON (or indeed, any other type), they just issue a 302 redirect.

这不起作用(因为它是跨域):

This doesn't work (because it's cross domain):

jQuery.getJSON('https://api.twitter.com/1/users/profile_image/60173.json',
function(data, status, jqxhr){
  console.log("data: ", data);
  console.log("status: ", status);
  console.log("jqxhr: ", jqxhr);
  console.log(jqxhr.getResponseHeader('Location'));
  console.log(jqxhr.getAllResponseHeaders());
})

数据为空,状态为'success',jqhxr返回jqxhr对象,但getResponseHeader()和getResponseHeaders()为null,因为它是跨域调用而不是真正的XMLHTTP打电话。

Data is empty, status is 'success', jqhxr returns a jqxhr object, but the getResponseHeader() and getResponseHeaders() are null because it's a cross-domain call rather than a true XMLHTTP call.

所以我想我可以使用load()事件来显示我的实际网址法师在302发生之后。我试过这个:

So I thought I might be able to use the load() event to show the actual url of the image after the 302 had happened. I tried this:

function avatar_loaded(img) {
  console.log("We loaded an avatar for %s (img)[0].src is %o
attr('src') is %o getAttribute('src') is %o", img, jQuery(img)[0].src, jQuery(img).attr('src'), img.getAttribute("src"));
}

var avatar_url = "https://api.twitter.com/1/users/profile_image/60173";

jQuery('body').append("<img onload='avatar_loaded(this)' src='" +
avatar_url + "' width='0' height='0'>");

你会看到avatar_loaded函数被调用两次。一旦使用了初始的img src(https://api.twitter.com/1/users/profile_image/60173),然后再次使用302ed实际图像url http://a1.twimg.com/profile_images/1272489576/Photo_38_normal.jpg

You'll see that the avatar_loaded function gets called twice. Once with the initial img src (https://api.twitter.com/1/users/profile_image/60173) and then again, presumably with the 302ed actual image url http://a1.twimg.com/profile_images/1272489576/Photo_38_normal.jpg

但无论我尝试什么,我都无法读取重定向的绝对URL。还有其他任何想法吗?

But I can't read the redirected absolute URL no matter what I try. Any other ideas?

编辑:我不想把它变成时间接收器,所以我只是用了

I Didn't want this to turn into a time-sink, so I just used the

http://api.twitter.com/1/users/show.json?user_id=

API调用。我想象这需要对受保护用户进行经过身份验证的调用,但它并不是那么好用。

API call. I'd imagined that this would require an authenticated call for protected users, it doesn't so it's fine to use.

推荐答案

而不是使用

http://api.twitter.com/1/users/profile_image/<user_id>

API调用,我用过

http://api.twitter.com/1/users/show.json?user_id=

而是从那里提取头像URL。它不是轻量级的,因为它也会返回大量其他用户数据,但即使是受保护的用户头像也无需验证即可返回,因此它是一个合适的替代品。

instead and extracted the avatar URL from there. It's not as light-weight because it returns a raft of other user data too, but even protected users avatars are returned without the need for authentication, so it's a suitable replacement.

这篇关于读取图像的绝对重定向SRC属性URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆