[英]Having trouble getting jQuery AJAX response headers
這就是我正在嘗試的:
$.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
我在這里找到了一種解決方案: 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 到 go 並從診斷中獲得最終重定向 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
是 JSONP 調用嗎? 那時你不會得到任何標題。 另請參閱: jqXHR.getAllResponseHeaders() 不會返回所有標頭
確保您使用 > jQuery 1.5 並確保將 crossDomain:true 添加到您的 ajax 屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.