<?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>JackKozik.com &#187; deferred object</title> <atom:link href="http://jackkozik.com/tag/deferred-object/feed" rel="self" type="application/rss+xml" /><link>http://jackkozik.com</link> <description>Web Programming, Home Networking and Personal Travel</description> <lastBuildDate>Mon, 04 Sep 2023 14:26:56 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.9.2</generator> <item><title>Jquery ajax always parameters order inconsistent</title><link>http://jackkozik.com/jquery-ajax-always-parameters-order-inconsistent/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jquery-ajax-always-parameters-order-inconsistent</link> <comments>http://jackkozik.com/jquery-ajax-always-parameters-order-inconsistent/#comments</comments> <pubDate>Tue, 16 Apr 2013 21:15:55 +0000</pubDate> <dc:creator><![CDATA[Jack Kozik]]></dc:creator> <category><![CDATA[Web Programming]]></category> <category><![CDATA[$.ajax]]></category> <category><![CDATA[always]]></category> <category><![CDATA[deferred object]]></category> <category><![CDATA[Jquery]]></category> <category><![CDATA[promise methods]]></category><guid isPermaLink="false">http://jackkozik.com/?p=478</guid> <description><![CDATA[<p>My single page web application makes several REST style calls to my server.  Jquery $.ajax is the tool of choice for implementing this on the browser-side.  When converting from Jquery version 1.4 to 1.7+ I started to follow a their new coding pattern.  Here&#8217;s the old way, the new pattern and a little gotcha (the [&#8230;]</p><p>The post <a rel="nofollow" href="http://jackkozik.com/jquery-ajax-always-parameters-order-inconsistent/">Jquery ajax always parameters order inconsistent</a> appeared first on <a rel="nofollow" href="http://jackkozik.com">JackKozik.com</a>.</p> ]]></description> <content:encoded><![CDATA[<p>My single page web application makes several REST style calls to my server.  Jquery $.ajax is the tool of choice for implementing this on the browser-side.  When converting from Jquery version 1.4 to 1.7+ I started to follow a their new coding pattern.  Here&#8217;s the old way, the new pattern and a little gotcha (the jquery ajax always parameters are inconsistent depending on success condition) that I write-up here for my reference and potentially the benefit of others.</p><p>Old mode of operation using jquery 1.4 $.ajax syntax:</p><pre class="brush: css; title: ; notranslate">
$.ajax({"url":RestURI,
	"type":"GET",
	"dataType":"text",
	success: function(data) {
		// Do success stuff
		// stop logging timer, do logging
		// stop spinnger            
	}, /* end success: */
	
	error: function (req, stat, err) {    
		// Do error stuff        
		// stop logging timer, do logging
		// stop spinner
	} /* end error: */
});
</pre><p>New pattern using jquery 1.7 uses the chainable done(), fail(), and always() Promise methods derived from the  <a href="http://api.jquery.com/category/deferred-object/">jQuery Deferred object</a>. .See  code snippet below for the general case for how I use it.  It&#8217;s pretty straight forward.</p><pre class="brush: css; title: ; notranslate">
// Start spinner
// start logging timer	 
$.ajax({"url":RestURI,
	"type":"GET",
	"dataType":"text"})
	.done( function(data, textStatus, jqXHR) {
		// Do success stuff
	}) /* end done: */
	
	.fail( function (jqXHR, textStatus, err) {
		// Do error stuff    
	}) /* end fail */
	
	.always( function() {
		// stop logging timer, do logging
		// stop spinner
	}); /* End always */
</pre><p>The new $.ajax pattern lets me have multiple always, done and fails; so I thought it would be good to add a .always at the top &#8212; something I would run first before a done or fail. Well, I discovered that the ajax always parameters are inconsistent; that is, the always function returns two different sets of parameters depending on how the $.ajax function returned. If the ajax function failed, then the always callback parameters were (jqXHR, textStatus, err ); if the ajax function was successful, the always callback parameters were flipped: (err, textStatus, jqXHR ).</p><p>So if you want to use the always function, then I recommend you setup a callback with no parameters or you do a test of the textStatus parameter &#8212; &#8220;success&#8221; This quirk got me a couple of times. I found <a href="http://bugs.jquery.com/ticket/11548">references to it on the jQuery bug list</a> &#8212; this behavior is by design. The code snippet below captures the issue.  This is not a show stopper; the new promise paradigm is pretty cool and I have some cases where I am chaining together multiple ajax calls, where this will come in really handy.</p><pre class="brush: css; title: ; notranslate">
// Start spinner
// start logging timer	 
$.ajax({"url":RestURI,
	"type":"GET",
	"dataType":"text"})
	.always( function(arg1, textStatus, arg3 ) {
		// on fail: always( function(jqXHR, textStatus, err ) { and textStatus != "success"
		// on done: always( function(err, textStatus, jqXHR ) { and textStatus == "success"
		console.log("First Always function. textStatus="+textStatus+"\n");
	}) /* end always (top) */
	
	.done( function(data, textStatus, jqXHR) {
		// Do success stuff
	}) /* end done: */
	
	.fail( function (jqXHR, textStatus, err) {
		// Do error stuff    
	}) /* end fail */
	
	.always( function() {
		// stop logging timer, do logging
		// stop spinner
	}); /* End always */
</pre><p>The post <a rel="nofollow" href="http://jackkozik.com/jquery-ajax-always-parameters-order-inconsistent/">Jquery ajax always parameters order inconsistent</a> appeared first on <a rel="nofollow" href="http://jackkozik.com">JackKozik.com</a>.</p> ]]></content:encoded> <wfw:commentRss>http://jackkozik.com/jquery-ajax-always-parameters-order-inconsistent/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>