Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to display a JSON error message?
    primarykey
    data
    text
    <p>I'm currently developing a tumblr theme and have built a jQuery JSON thingamabob that uses the Tumblr API to do the following:</p> <p>The user would click on the "post type" link (e.g. Video Posts), at which stage jQuery would use JSON to grab all the posts that's related to that type and then dynamically display them in a designated area.</p> <p>Now everything works absolutely peachy, except that with Tumblr being Tumblr and their servers taking a knock every now and then, the Tumblr API thingy is sometimes offline. Now I can't foresee when this function will be down, which is why I want to display some generic error message if JSON (for whatever reason) was unable to load the post.</p> <p>You'll see I've already written some code to show an error message when jQuery can't find any posts related to that post type BUT it doesn't cover any server errors. Note: I sometimes get this error:</p> <p><strong>Failed to load resource: the server responded with a status of 503 (Service Temporarily Unavailable)</strong></p> <p>It is for this 503 Error message that I need to write some code, but I'm slightly clueless :)</p> <p>Here's the jQuery JSON code:</p> <pre><code>$('ul.right li').find('a').click(function() { var postType = this.className; var count = 0; byCategory(postType); return false; function byCategory(postType, callback) { $.getJSON('{URL}/api/read/json?type=' + postType + '&amp;callback=?', function(data) { var article = []; $.each(data.posts, function(i, item) { // i = index // item = data for a particular post switch(item.type) { case 'photo': article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="photo" style="padding-bottom:5px;"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;&lt;img src="http://static.tumblr.com/ewjv7ap/XSTldh6ds/photo_icon.png" alt="type_icon"/&gt;&lt;/a&gt;' + '&lt;a href="' + item.url + '" title="{Title}"&gt;&lt;img src="' + item['photo-url-500'] + '"alt="image" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;'; count = 1; break; case 'video': article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="video" style="padding-bottom:5px;"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;' + '&lt;img src="http://static.tumblr.com/ewjv7ap/nuSldhclv/video_icon.png" alt="type_icon"/&gt;&lt;/a&gt;' + '&lt;span style="margin: auto;"&gt;' + item['video-player'] + '&lt;/span&gt;' + '&lt;/div&gt;&lt;/div&gt;'; count = 1; break; case 'audio': if (use_IE == true) { article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="regular"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;&lt;img src="http://static.tumblr.com/ewjv7ap/R50ldh5uj/audio_icon.png" alt="type_icon"/&gt;&lt;/a&gt;' + '&lt;h3&gt;&lt;a href="' + item.url + '"&gt;' + item['id3-artist'] +' - ' + item['id3-title'] + '&lt;/a&gt;&lt;/h3&gt;' + '&lt;/div&gt;&lt;/div&gt;'; } else { article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="regular"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;&lt;img src="http://static.tumblr.com/ewjv7ap/R50ldh5uj/audio_icon.png" alt="type_icon"/&gt;&lt;/a&gt;' + '&lt;h3&gt;&lt;a href="' + item.url + '"&gt;' + item['id3-artist'] +' - ' + item['id3-title'] + '&lt;/a&gt;&lt;/h3&gt;&lt;div class="player"&gt;' + item['audio-player'] + '&lt;/div&gt;' + '&lt;/div&gt;&lt;/div&gt;'; }; count = 1; break; case 'regular': article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="regular"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;&lt;img src="http://static.tumblr.com/ewjv7ap/dwxldhck1/regular_icon.png" alt="type_icon"/&gt;&lt;/a&gt;&lt;h3&gt;&lt;a href="' + item.url + '"&gt;' + item['regular-title'] + '&lt;/a&gt;&lt;/h3&gt;&lt;div class="description_container"&gt;' + item['regular-body'] + '&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;'; count = 1; break; case 'quote': article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="quote"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;&lt;img src="http://static.tumblr.com/ewjv7ap/loEldhcpr/quote_icon.png" alt="type_icon"/&gt;&lt;/a&gt;&lt;blockquote&gt;&lt;h3&gt;&lt;a href="' + item.url + '" title="{Title}"&gt;' + item['quote-text'] + '&lt;/a&gt;&lt;/h3&gt;&lt;/blockquote&gt;&lt;cite&gt;- ' + item['quote-source'] + '&lt;/cite&gt;&lt;/div&gt;&lt;/div&gt;'; count = 1; break; case 'conversation': article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="chat"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;&lt;img src="http://static.tumblr.com/ewjv7ap/MVuldhcth/conversation_icon.png" alt="type_icon"/&gt;&lt;/a&gt;&lt;h3&gt;&lt;a href="' + item.url + '"&gt;' + item['conversation-title'] + '&lt;/a&gt;&lt;/h3&gt;&lt;/div&gt;&lt;/div&gt;'; count = 1; break; case 'link': article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="link"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;&lt;img src="http://static.tumblr.com/ewjv7ap/EQGldhc30/link_icon.png" alt="type_icon"/&gt;&lt;/a&gt;&lt;h3&gt;&lt;a href="' + item['link-url'] + '" target="_blank"&gt;' + item['link-text'] + '&lt;/a&gt;&lt;/h3&gt;&lt;/div&gt;&lt;/div&gt;'; count = 1; break; default: alert('No Entries Found.'); }; }) // end each if (!(count == 0)) { $('#content_right') .hide('fast') .html('&lt;div class="first_div"&gt;&lt;span class="left_corner"&gt;&lt;/span&gt;&lt;span class="right_corner"&gt;&lt;/span&gt;&lt;h2&gt;Displaying ' + postType + ' Posts Only&lt;/h2&gt;&lt;/div&gt;' + article.join('')) .slideDown('fast') } else { $('#content_right') .hide('fast') .html('&lt;div class="first_div"&gt;&lt;span class="left_corner"&gt;&lt;/span&gt;&lt;span class="right_corner"&gt;&lt;/span&gt;&lt;h2&gt;Hmmm, currently there are no ' + postType + ' posts to display&lt;/h2&gt;&lt;/div&gt;') .slideDown('fast') } // end getJSON }); // end byCategory } }); </code></pre> <p>If you'd like to see the demo in action, check out <a href="http://elegantem.tumblr.com/" rel="nofollow">Elegantem</a> but do note that everything might work absolutely fine for you (or not), depending on Tumblr's temperament.</p> <p><hr /> <strong>Update</strong> Okay, so after following jmorts answer underneath as close to the letter as 2am allows, I've churned out the following code without success - there's no alert popping up. Myabe I'm a muppet, maybe I'm just scheleeeepy but if you jedi folks can take another peek I'd really appreciate it :)</p> <pre><code>$('ul.right li').find('a').click(function() { var postType = this.className; var count = 0; byCategory(postType); return false; function byCategory(postType, callback) { $.getJSON('{URL}/api/read/json?type=' + postType + '&amp;callback=?', function(data, textStatus, xhr) { // main callback function if(xhr.status == 500 || xhr.status == 404 || xhr.status == 503) { yourErrorHandler(data, textStatus, xhr); // success } else { yourCallbackToRunIfSuccessful(data); // failed } } ); function yourCallbackToRunIfSuccessful(data) { var article = []; $.each(data.posts, function(i, item) { // i = index // item = data for a particular post switch(item.type) { case 'photo': article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="photo" style="padding-bottom:5px;"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;&lt;img src="http://static.tumblr.com/ewjv7ap/XSTldh6ds/photo_icon.png" alt="type_icon"/&gt;&lt;/a&gt;' + '&lt;a href="' + item.url + '" title="{Title}"&gt;&lt;img src="' + item['photo-url-500'] + '"alt="image" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;'; count = 1; break; case 'video': article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="video" style="padding-bottom:5px;"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;' + '&lt;img src="http://static.tumblr.com/ewjv7ap/nuSldhclv/video_icon.png" alt="type_icon"/&gt;&lt;/a&gt;' + '&lt;span style="margin: auto;"&gt;' + item['video-player'] + '&lt;/span&gt;' + '&lt;/div&gt;&lt;/div&gt;'; count = 1; break; case 'audio': if (use_IE == true) { article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="regular"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;&lt;img src="http://static.tumblr.com/ewjv7ap/R50ldh5uj/audio_icon.png" alt="type_icon"/&gt;&lt;/a&gt;' + '&lt;h3&gt;&lt;a href="' + item.url + '"&gt;' + item['id3-artist'] +' - ' + item['id3-title'] + '&lt;/a&gt;&lt;/h3&gt;' + '&lt;/div&gt;&lt;/div&gt;'; } else { article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="regular"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;&lt;img src="http://static.tumblr.com/ewjv7ap/R50ldh5uj/audio_icon.png" alt="type_icon"/&gt;&lt;/a&gt;' + '&lt;h3&gt;&lt;a href="' + item.url + '"&gt;' + item['id3-artist'] +' - ' + item['id3-title'] + '&lt;/a&gt;&lt;/h3&gt;&lt;div class="player"&gt;' + item['audio-player'] + '&lt;/div&gt;' + '&lt;/div&gt;&lt;/div&gt;'; }; count = 1; break; case 'regular': article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="regular"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;&lt;img src="http://static.tumblr.com/ewjv7ap/dwxldhck1/regular_icon.png" alt="type_icon"/&gt;&lt;/a&gt;&lt;h3&gt;&lt;a href="' + item.url + '"&gt;' + item['regular-title'] + '&lt;/a&gt;&lt;/h3&gt;&lt;div class="description_container"&gt;' + item['regular-body'] + '&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;'; count = 1; break; case 'quote': article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="quote"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;&lt;img src="http://static.tumblr.com/ewjv7ap/loEldhcpr/quote_icon.png" alt="type_icon"/&gt;&lt;/a&gt;&lt;blockquote&gt;&lt;h3&gt;&lt;a href="' + item.url + '" title="{Title}"&gt;' + item['quote-text'] + '&lt;/a&gt;&lt;/h3&gt;&lt;/blockquote&gt;&lt;cite&gt;- ' + item['quote-source'] + '&lt;/cite&gt;&lt;/div&gt;&lt;/div&gt;'; count = 1; break; case 'conversation': article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="chat"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;&lt;img src="http://static.tumblr.com/ewjv7ap/MVuldhcth/conversation_icon.png" alt="type_icon"/&gt;&lt;/a&gt;&lt;h3&gt;&lt;a href="' + item.url + '"&gt;' + item['conversation-title'] + '&lt;/a&gt;&lt;/h3&gt;&lt;/div&gt;&lt;/div&gt;'; count = 1; break; case 'link': article[i] = '&lt;div class="post_wrap"&gt;&lt;div class="link"&gt;' + '&lt;a href="' + item.url + '" title="{Title}" class="type_icon"&gt;&lt;img src="http://static.tumblr.com/ewjv7ap/EQGldhc30/link_icon.png" alt="type_icon"/&gt;&lt;/a&gt;&lt;h3&gt;&lt;a href="' + item['link-url'] + '" target="_blank"&gt;' + item['link-text'] + '&lt;/a&gt;&lt;/h3&gt;&lt;/div&gt;&lt;/div&gt;'; count = 1; break; default: alert('No Entries Found.'); }; }) // end each if (!(count == 0)) { $('#content_right') .hide('fast') .html('&lt;div class="first_div"&gt;&lt;span class="left_corner"&gt;&lt;/span&gt;&lt;span class="right_corner"&gt;&lt;/span&gt;&lt;h2&gt;Displaying ' + postType + ' Posts Only&lt;/h2&gt;&lt;/div&gt;' + article.join('')) .slideDown('fast') } else { $('#content_right') .hide('fast') .html('&lt;div class="first_div"&gt;&lt;span class="left_corner"&gt;&lt;/span&gt;&lt;span class="right_corner"&gt;&lt;/span&gt;&lt;h2&gt;Hmmm, currently there are no ' + postType + ' posts to display&lt;/h2&gt;&lt;/div&gt;') .slideDown('fast') } // end getJSON }; // end byCategory function yourErrorHandler(data,textStatus,xhr) { alert("Server returned status code " + xhr.status + ". Try again later."); } } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload