2

我已经阅读了几十篇关于如何正确处理来自 jQuery AJAX 调用的 HTTP 状态代码的文章,但我完全失败了。也许有一个错误?如果在其他地方回答了这个问题,请原谅我,但我根本无法让它发挥作用。

我正在尝试使用 Musicbrainz 发行版 UUID 从封面艺术档案中加载专辑插图。如果没有艺术作品,他们的服务器返回 404,如果他们直接返回 JSON,则返回 200,或者更常见的是,如果他们重定向到 JSON(带有最终图像信息),则返回 307。

我使用 PHP/CURL 在服务器端进行这项工作,但它会导致页面加载延迟很长(因为服务器阻塞等待来自coverartarchive.org 的响应)。我想让客户端异步加载专辑插图。

这就是我所拥有的 - 请注意,只调用错误回调;没有调用任何 statusCode 函数。我已经尝试了 umteen 不同的版本。没有工作。我读过浏览器拦截了 307 重定向的问题,但甚至没有命中 404 回调。

 $('.album_art').each(function() {
    var me = $(this);
    var uuid = me.data("uuid");
    var artistName = me.data("artist");
    var trackName = me.data("track");

    console.debug("Loading cover art for track "+uuid+ " artist name and track name: " + artistName, trackName);

    $.ajax({
        type: 'GET',
        cache: 'FALSE',
        //contentType: "application/json",
        url: "http://coverartarchive.org/release/"+uuid,
        //data: jsonString,
        statusCode: {
            200: function () { 
                console.debug("200 for " + uuid);
            },
            307: function () {
                console.debug("307 for " + uuid);
            },
            404: function () {
                console.debug("404 for " + uuid);
            }
        }
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.debug("error for " + uuid);
        }
    });
  }); // sorry, indentation is a bit out after copy/paste

来自 Firebug 控制台输出的一些示例专辑/版本的片段:

Loading cover art for track 695ca8ba-16f3-4231-bf03-6ba81d435b72 artist name and track name: Nirvana Smells Like Teen Spirit
GET http://coverartarchive.org/release/695ca8ba-16f3-4231-bf03-6ba81d435b72 
307 TEMPORARY REDIRECT  239ms 

Loading cover art for track 49364063-bcb6-4d9f-b2bc-940f76495db9 artist name and track name: INXS Kiss the Dirt (Falling Down the Mountain)
GET http://coverartarchive.org/release/49364063-bcb6-4d9f-b2bc-940f76495db9
404 NOT FOUND  249ms 

Loading cover art for track b09e7fcf-0383-4e7c-acb0-d5ab437d6713 artist name and track name: Midnight Oil Blot
GET http://coverartarchive.org/release/b09e7fcf-0383-4e7c-acb0-d5ab437d6713
404 NOT FOUND   285ms 

Loading cover art for track 90ab8c15-08c8-424e-9d18-62f2f35dae7a artist name and track name: Macklemore & Ryan Lewis feat. Wanz Thrift Shop
GET http://coverartarchive.org/release/90ab8c15-08c8-424e-9d18-62f2f35dae7a
307 TEMPORARY REDIRECT   285ms 

Loading cover art for track 433ffa89-1e6a-434a-9bfe-6690f3b6e32a artist name and track name: Them Crooked Vultures Mind Chaser, No Eraser
GET http://coverartarchive.org/release/433ffa89-1e6a-434a-9bfe-6690f3b6e32a
404 NOT FOUND   286ms

Loading cover art for track bc091f8f-17cc-4ad3-8ca3-a7d080c61e1d artist name and track name: Nine Inch Nails The Good Soldier
GET http://coverartarchive.org/release/bc091f8f-17cc-4ad3-8ca3-a7d080c61e1d
404 NOT FOUND   296ms
error for 7b382603-3fda-47d0-8b86-8c661ad616ca
error for 909fbadc-46a5-4bd6-864b-e36d9b47a98e
error for 39ec579c-7a31-4edd-9fd5-d1f48731bf92
GET http://archive.org/download/mbid-dfeacac3-16f0-4ef7-94de-e25679b050c2/index.json
302 Moved Temporarily   1.39s   
error for 49364063-bcb6-4d9f-b2bc-940f76495db9
error for b09e7fcf-0383-4e7c-acb0-d5ab437d6713
error for 433ffa89-1e6a-434a-9bfe-6690f3b6e32a
GET http://archive.org/download/mbid-b776a4df-e8a4-4f23-838a-600b5db75ad3/index.json
302 Moved Temporarily   1.45s
GET http://archive.org/download/mbid-69acc6e3-7fe6-4c8c-991e-860677dcf0db/index.json
302 Moved Temporarily    1.63s
error for bc091f8f-17cc-4ad3-8ca3-a7d080c61e1d
error for 4b5657bb-6209-493a-9e64-2bae4c3d4f1a

特别是这看起来像完全相同的问题: jquery ajax statusCode: 502 function never called

4

1 回答 1

2

我用下面的代码做了一个小提琴来显示 statusCode 是 0:http: //jsfiddle.net/brGTj/

uuid = "90ab8c15-08c8-424e-9d18-62f2f35dae7a"

$.ajax({
    type: 'GET',
    cache: 'FALSE',
    //contentType: "application/json",
    url: "http://coverartarchive.org/release/"+uuid,
    //data: jsonString,
    statusCode: {
        200: function () { 
            console.debug("200 for " + uuid);
        },
        307: function () {
            console.debug("307 for " + uuid);
        },
        404: function () {
            console.debug("404 for " + uuid);
        },
        0: function() {
            console.debug("0 for " + uuid);
        }
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        console.debug("error for " + uuid);
        console.log(XMLHttpRequest);
    }
});

你在为coverartarchive.org 编写代码吗?如果没有,它可能会被同源策略阻止。

于 2013-07-04T22:54:12.753 回答