Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 读取图像的绝对重定向SRC属性URL_Javascript_Jquery_Image_Redirect_Twitter - Fatal编程技术网

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

Javascript 读取图像的绝对重定向SRC属性URL,javascript,jquery,image,redirect,twitter,Javascript,Jquery,Image,Redirect,Twitter,我正在使用Twitter头像API在客户端获取用户的头像URL,我想缓存响应,但它们只是发出302重定向,而不是常规的JSON(或者任何其他类型) 这不起作用(因为它是跨域的): 数据为空,状态为“成功”,jqhxr返回jqxhr对象,但getResponseHeader()和getResponseHeaders()为空,因为它是跨域调用,而不是真正的XMLHTTP调用 所以我想我可以使用load()事件来显示302事件发生后图像的实际url。我试过这个: function avatar_loa

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

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

数据为空,状态为“成功”,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("<img onload='avatar_loaded(this)' src='" +
avatar_url + "' width='0' height='0'>");

API调用。我认为这需要对受保护的用户进行身份验证调用,但事实并非如此,因此可以使用。

以下是只加载一次的代码:

avatar\u加载=功能(img){
console.log(“我们为%s(img)[0]加载了一个化身。src是%o attr('src')是%o getAttribute('src')是%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";
$(窗口)。加载(函数(){
var img=新图像();
$(img).load(function(){avatar_loaded(this)}).attr('src',avatar_url).appendTo('body');
//jQuery('body')。追加(“”);
});
而不是使用

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

取而代之的是从那里提取了化身URL。它没有那么轻,因为它也会返回大量其他用户数据,但即使是受保护的用户头像也会在不需要身份验证的情况下返回,因此它是一个合适的替代品。

它会被调用两次,但具有相同的src:感谢单一负载变化,但是我想做的是获取重定向的图像URL-在console.log中,您会看到src与html相同,而不是我想读的重定向URL为什么需要绝对URL?因为api.twitter.com/1/users/profile_image/URL是一个受速率限制的twitter api调用。最佳做法是调用一次,将答案缓存一段时间,然后使用缓存的回复。
http://api.twitter.com/1/users/show.json?user_id=
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("<img onload='' src='" + avatar_url + "' width='100' height='100'>");
});
http://api.twitter.com/1/users/profile_image/<user_id>
http://api.twitter.com/1/users/show.json?user_id=