3

これは私が試していることです:

$.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で確認できる応答ヘッダーです。

サーバー:nginx
日付:2011年7月2日土曜日03:04:26 GMT
コンテンツタイプ:text / html; charset = utf-8
転送エンコーディング:チャンク
接続:閉じる
Set-Cookie:IMGURSESSION = asdfasdfasdfasdf; パス=/; domain = .imgur.com
SERVERID = www4; パス=/
有効期限:1981年11月19日木曜日08:52:00 GMT
キャッシュ制御:no-store、no-cache、must-revalidate、post-check = 0、pre-check = 0
プラグマ:キャッシュなし
場所:http://imgur.com/ocuVX
コンテンツエンコーディング:gzip
変更:Accept-エンコーディング
4

3 に答える 3

5

私はここで1つの解決策を見つけました: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キーレス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
于 2011-07-02T05:28:56.510 に答える
2

JSONP呼び出しですか?その場合、ヘッダーは取得されません。これも参照してください: jqXHR.getAllResponseHeaders() はすべてのヘッダーを返しません

于 2011-07-02T03:00:08.060 に答える
0

> jQuery 1.5 を使用していることを確認し、ajax 属性に crossDomain:true を追加していることを確認してください

于 2011-07-02T03:50:09.203 に答える