读取图像的绝对重定向SRC属性URL

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

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

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

数据为空,状态为“成功”,jqhxr返回jqxhr对象,但getResponseHeader()和getResponseHeaders()为空,因为它是跨域调用而不是真正的XMLHTTP调用。

所以我想我可以使用load()事件来显示302发生后图像的实际url。 我试过这个:

 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(""); 

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

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

编辑:我不希望这变成一个时间沉,所以我只是使用了

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

API调用。 我想象这需要对受保护用户进行经过身份validation的调用,但事实并非如此。

而不是使用

 http://api.twitter.com/1/users/profile_image/ 

API调用,我用过

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

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

这里的代码只加载一次: http : //jsfiddle.net/ionutzp/6Ekub/1/

 avatar_loaded = function(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"; $(window).load(function(){ var img = new Image(); $(img).load(function(){avatar_loaded(this)}).attr('src',avatar_url).appendTo('body'); //jQuery('body').append(""); });