获取 jQuery AJAX 响应标头时遇到问题

发布于 2024-11-17 22:23:56 字数 852 浏览 5 评论 0 原文

这就是我正在尝试的:

$.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

This is what 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 just get an empty string.

Any help would be appreciated.

Edit:

These are the response headers I 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舞袖。长 2024-11-24 22:23:56

我在这里找到了一个解决方案: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

I've found one solution here: 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);
 }

Obviously this solution requires a POST which means you need to use an API key. I couldn't find any way of getting a response with the API-keyless GET method.

The only way I could manage to do an upload without an API key was to go through YQL and get the final redirect URL from the 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 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
痞味浪人 2024-11-24 22:23:56

是 JSONP 调用吗?那么你将不会得到任何标题。另请参阅:jqXHR.getAllResponseHeaders() 不会返回所有标头

Is it JSONP call? You won't get any headers then. Also see this: jqXHR.getAllResponseHeaders() won't return all headers

最笨的告白 2024-11-24 22:23:56

确保您使用的是 > jQuery 1.5 并确保将 crossDomain:true 添加到 ajax 属性中

Make sure you're using > jQuery 1.5 and make sure you add crossDomain:true to your ajax attributes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文