这就是我正在尝试的:
$.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());
}
});
我只是得到一个空字符串。
任何帮助都将不胜感激。
修改
这些是我在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
答案 0 :(得分:5)
我在这里找到了一个解决方案:https://hacks.mozilla.org/2011/03/the-shortest-image-uploader-ever/
function upload(url) {
// Let's build a FormData object
var fd = new FormData();
fd.append("image", url); // Append the file
fd.append("key", "6528448c258cff474ca9701c5bab6927");
// Get your own key: http://api.imgur.com/
// Create the 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!
// The URL of the image is:
JSON.parse(xhr.responseText).upload.links.imgur_page;
}
// Ok, I don't handle the errors. An exercice for the reader.
// And now, we send the formdata
xhr.send(fd);
}
显然,此解决方案需要POST,这意味着您需要使用API密钥。我找不到任何方法来获得API-keyless GET方法的响应。
我可以设法在没有API密钥的情况下进行上传的唯一方法是通过YQL并从诊断中获取最终的重定向URL:
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 from html where url='#{upload_url}'"
diagnostics: true
success: (data) ->
redirects = data.query.diagnostics.redirect
image_url = redirects[redirects.length-1].content
callback image_url
答案 1 :(得分:1)
是JSONP电话吗?那你就不会得到任何标题。另请参阅:jqXHR.getAllResponseHeaders() won't return all headers
答案 2 :(得分:0)
确保您使用> jQuery 1.5并确保将crossDomain:true添加到您的ajax属性