<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Less code, more sleep &#187; json</title>
	<atom:link href="http://emmett.be/thewebdev/tags/json/feed" rel="self" type="application/rss+xml" />
	<link>http://emmett.be/thewebdev</link>
	<description>Tales of an insomniac coder</description>
	<lastBuildDate>Mon, 26 Jul 2010 22:48:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Improved jquery ajax error reporting</title>
		<link>http://emmett.be/thewebdev/archives/64</link>
		<comments>http://emmett.be/thewebdev/archives/64#comments</comments>
		<pubDate>Sat, 12 Dec 2009 16:02:22 +0000</pubDate>
		<dc:creator>Emmett Pickerel</dc:creator>
				<category><![CDATA[Frontend]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://emmett.be/thewebdev/?p=64</guid>
		<description><![CDATA[jQuery is a great library, and I highly recommend it after 6 months of use. One of the problems I&#8217;ve had with it, however, is its poor error reporting on ajax requests. While you can subscribe to the global event ajaxError, there&#8217;s nothing to tell you what went wrong. Specifically, I&#8217;ve wanted a way to ]]></description>
			<content:encoded><![CDATA[<p>jQuery is a great library, and I highly recommend it after 6 months of use. One of the problems I&#8217;ve had with it, however, is its poor error reporting on ajax requests. While you can subscribe to the global event <code>ajaxError</code>, there&#8217;s nothing to tell you <em>what</em> went wrong. Specifically, I&#8217;ve wanted a way to report when the json parse fails.</p>
<p>Looking through the jQuery core, I found that the problem is that &#8220;status&#8221; field isn&#8217;t sent to the handler. A simple intervention on <code>jQuery.handleError</code> will close the gap:</p>
<pre>jQuery.handleErrorOriginal = $.handleError;
jQuery.handleError = function( s, xhr, status, e ){
  if (s.global) {
    xhr.error_status = status;
  }
  jQuery.handleErrorOriginal.apply(this, arguments);
};</pre>
<p>The &#8220;e&#8221; object is the original ajax configuration object. Since it reaches the global ajaxError listener, we can pull the status out of that.</p>
<p>Notice that I don&#8217;t <em>replace</em> the original functionality, I just do something before calling it with the same scope and arguments. The reason here is forwards-compatibility. Assuming that the name and api of jQuery.handleError doesn&#8217;t change, any changes to its internal functionality in future updates will likely be preserved without refactoring effort on your part.</p>
<p>To catch the errors:</p>
<pre>$("html").ajaxError(function(xhr, s, e){
    var msg;
    switch (s.error_status) {
      case "parsererror":
        msg = "A JSON parsing error occurred.";
        break;
      case "timeout":
        msg = "An ajax request timed out.";
        break;
      default:
        msg = "An ajax error occurred.";
        break;
    }
    alert(msg + "\n\n" + e.url);
});</pre>
<p>Why do I set <code>xhr.error_status</code> and then read <code>s.error_status</code>? Because, somewhere in the jumble of <code>handleError</code> and <code>trigger</code>, <code>xhr</code> becomes <code>s</code>. I love jQuery, but some of its core code can be quite difficult to read through.</p>
]]></content:encoded>
			<wfw:commentRss>http://emmett.be/thewebdev/archives/64/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
