Note that there are some explanatory texts on larger screens.

plurals
  1. PORead the absolute redirected SRC attribute URL for an image
    primarykey
    data
    text
    <p>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.</p> <p>This doesn't work (because it's cross domain):</p> <pre><code>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()); }) </code></pre> <p>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.</p> <p>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:</p> <pre><code>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("&lt;img onload='avatar_loaded(this)' src='" + avatar_url + "' width='0' height='0'&gt;"); </code></pre> <p>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 <a href="http://a1.twimg.com/profile_images/1272489576/Photo_38_normal.jpg" rel="nofollow">http://a1.twimg.com/profile_images/1272489576/Photo_38_normal.jpg</a></p> <p>But I can't read the redirected absolute URL no matter what I try. Any other ideas?</p> <p><strong>EDIT:</strong> I Didn't want this to turn into a time-sink, so I just used the</p> <pre><code>http://api.twitter.com/1/users/show.json?user_id= </code></pre> <p>API call. I'd imagined that this would require an authenticated call for protected users, it doesn't so it's fine to use.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload