javascript - Having trouble getting jQuery AJAX response headers -
this i'm trying:
$.ajax({ type: 'get', url: 'http://imgur.com/upload/', data: { url: 'http://upload.wikimedia.org/wikipedia/commons/3/3e/phalaenopsis_jpeg.png' }, complete: function(jqxhr, textstatus) { console.log(jqxhr.getallresponseheaders()); } });
i empty string.
any appreciated.
edit:
these response headers can see in firebug:
server: nginx date: sat, 02 jul 2011 03:04:26 gmt content-type: text/html; charset=utf-8 transfer-encoding: chunked connection: close set-cookie: imgursession=asdfasdfasdfasdf; path=/; domain=.imgur.com serverid=www4; path=/ expires: thu, 19 nov 1981 08:52:00 gmt cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 pragma: no-cache location: http://imgur.com/ocuvx content-encoding: gzip vary: accept-encoding
i've found 1 solution here: https://hacks.mozilla.org/2011/03/the-shortest-image-uploader-ever/
function upload(url) { // let's build formdata object var fd = new formdata(); fd.append("image", url); // append file fd.append("key", "6528448c258cff474ca9701c5bab6927"); // own key: http://api.imgur.com/ // create xhr (cross-domain xhr ftw!!!) var xhr = new xmlhttprequest(); xhr.open("post", "http://api.imgur.com/2/upload.json"); // boooom! xhr.onload = function() { // big win! // url of image is: json.parse(xhr.responsetext).upload.links.imgur_page; } // ok, don't handle errors. exercice reader. // , now, send formdata xhr.send(fd); }
obviously solution requires post means need use api key. couldn't find way of getting response api-keyless method.
the way manage upload without api key go through yql , final redirect url diagnostics:
urltoimgur = (url, callback) -> upload_url = "http://api.imgur.com/2/upload?url=#{url}" $.ajax url: 'http://query.yahooapis.com/v1/public/yql' datatype: 'jsonp' data: q: "select none html url='#{upload_url}'" diagnostics: true success: (data) -> redirects = data.query.diagnostics.redirect image_url = redirects[redirects.length-1].content callback image_url
Comments
Post a Comment