Reputation: 1309
I have been reading dozens of posts on how to correctly handle HTTP status codes from jQuery AJAX calls and am completely failing. Perhaps there's a bug? Forgive me if this is answered elsewhere but I simply cannot get this to work.
I'm trying to load album artwork from the Cover Art Archive using a Musicbrainz release UUID. Their server returns a 404 if there's no art, a 200 if they return a JSON directly or, more common, a 307 if they redirect to the JSON (with the ultimate image info).
I have this working on the server side using PHP/CURL but it causes a long delay in page load (as the server blocks waiting for responses from coverartarchive.org). I'd like to have the client load the album artwork asynchronously.
Here's what I have - note that only the error callback is called; none of the statusCode functions are called. I've tried umteen different versions of this. None work. I've read there are issues with 307 redirects being intercepted by the browser but not even the 404 callbacks are hit.
$('.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
Snippet from the Firebug console output for some sample albums/releases:
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
In particular this looks like the exact same issue: jquery ajax statusCode: 502 function never called
Upvotes: 2
Views: 4870
Reputation: 6352
I made a fiddle with the following code to show that the statusCode is 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);
}
});
Are you writing code for coverartarchive.org? If not, it's probably being stopped by the same origin policy.
Upvotes: 2