<?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>Web 3.0, 6 Bladed Razors, 7 Minute Abs &#187; Ajax</title>
	<atom:link href="http://www.zachleat.com/web/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zachleat.com/web</link>
	<description></description>
	<lastBuildDate>Fri, 30 Jul 2010 01:59:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Emulating onhashchange without setInterval</title>
		<link>http://www.zachleat.com/web/2008/08/21/onhashchange-without-setinterval/</link>
		<comments>http://www.zachleat.com/web/2008/08/21/onhashchange-without-setinterval/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 06:00:13 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Back Button]]></category>
		<category><![CDATA[History]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/?p=143</guid>
		<description><![CDATA[There is one limitation that all of the major JavaScript browser history management plugins have to hack around: How to tell when there is a change to the location.hash? Sure, you can tell when you&#8217;re modifying the hash yourself, but what if the user hits the back/forward button? YUI&#8217;s History component and Really Simple History [...]]]></description>
			<content:encoded><![CDATA[<p>There is one limitation that all of the major JavaScript browser history management plugins have to hack around:  How to tell when there is a change to the <code>location.hash</code>?  Sure, you can tell when you&#8217;re modifying the hash yourself, but what if the user hits the back/forward button?</p>
<p><a href="http://developer.yahoo.com/yui/history/">YUI&#8217;s History component</a> and Really Simple History both use setInterval with an internal variable to compare and find changes.  But this isn&#8217;t really an optimal solution.  As proper front end engineers, we should avoid timeouts as much as possible.  Internet Explorer 8 will have an <a href="http://msdn.microsoft.com/en-us/library/cc288209(VS.85).aspx">onhashchange event</a> that clients will be able to subscribe to.  That will be nice.  But surely, a cross browser solution without the use of setInterval exists.</p>
<p><strong>Look, a cross browser solution without the use of setInterval:</strong></p>
<ol>
<li>On initialization, we load an iframe onto the page that is positioned absolutely at -500px,-500px so the user can&#8217;t see it. It is a skeleton page that only needs cross browser code to add an &#8220;<code>onscroll</code>&#8221; event, and to be able to calculate the scrolled position of the iframe itself.  For my example, I use jQuery and the dimensions plugin to accomplish this, but it could easily be trimmed down to only the bare essentials (or ported to a different library).</li>
<li>To add an AJAX history entry into the browser&#8217;s history under an assigned hash string, we first add a <code>&lt;a name="hashString"&gt;hashString&lt;/a&gt;</code> to the <code>&lt;body&gt;</code> tag of the iframe.  Using css to increase the size of the a tag proportional to the iframe&#8217;s height, we can guarantee scrolling will happen.</li>
<li>Then, we change the <code>location.hash</code> of the iframe to point to that <code>&lt;a&gt;</code> tag.  This will scroll the iframe to the content, and create a new entry in the browser&#8217;s <code>history</code> object.</li>
<li>Inside the iframe, we have our <code>onscroll</code> event that fires when the scrolling in the previous step took place.  (Minor IE-related workaround: The browser&#8217;s <code>history</code> object is changed, but the hash property doesn&#8217;t when attempting to read it later.  Instead, we find the <code>&lt;a&gt;</code> that matches up with the scrollY/pageOffsetY property inside of the iframe, and retrieve the matching hash from the <code>&lt;a&gt;</code> tag.)</li>
</ol>
<p>The nice thing about this approach is that you don&#8217;t even need a history manager anymore.  This little iframe will do all of your dirty work for you.  And it will even maintain your history alongside any other iframe browsing on the page.</p>
<p><strong>Advantages:</strong></p>
<ul>
<li>Can serve as back button support and full AJAX history manager.</li>
<li>Page Weight: the test page and iframe HTML files together weigh in at 2.76 KB.  That includes the non-jQuery JavaScript needed to do everything.</li>
<li>Cross browser: Tested in FF3, IE7, IE6, Opera 9.5, (not Safari &#8212; see below)</li>
</ul>
<p><strong>Limitations:</strong></p>
<ul>
<li>No bookmarking support.  We aren&#8217;t changing the top hash, we&#8217;re changing the iframe hash, so these aren&#8217;t bookmarkable.</li>
</ul>
<p><strong>Sample Example:</strong></p>
<ul>
<li>
<a href="http://www.zachleat.com/Projects/history/">My Test Page</a></li>
<li><a href="http://www.zachleat.com/Projects/history/history.zip">Download the test page (2 HTML files, and jQuery 1.2.6 and dimensions)</a></li>
</ul>
<p><strong>Update</strong>: Given time for more rigorous testing, it doesn&#8217;t look like Safari supports this approach.  So, until WebKit fixes <a href="https://bugs.webkit.org/show_bug.cgi?id=9166">#9166</a>, we&#8217;ll have to stick to timers in Safari.  A more pragmatic programmer than I might hack around this approach by exploiting <a href="https://bugs.webkit.org/show_bug.cgi?id=19202">#19202</a>, but that certainly wouldn&#8217;t be a long term solution.  I&#8217;ve also updated the test page above, at <a href="http://dean.edwards.name/">Dean Edwards</a> humble request, to support dynamic client size text size changes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2008/08/21/onhashchange-without-setinterval/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Let the Door Hit You Onunload and Onbeforeunload</title>
		<link>http://www.zachleat.com/web/2008/04/22/dont-let-the-door-hit-you-onunload-and-onbeforeunload/</link>
		<comments>http://www.zachleat.com/web/2008/04/22/dont-let-the-door-hit-you-onunload-and-onbeforeunload/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 04:51:08 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[Interface Design]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web Browsers]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Gmail]]></category>
		<category><![CDATA[Opera]]></category>
		<category><![CDATA[Undo]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/?p=126</guid>
		<description><![CDATA[Many people attempt a last ditch effort to save page state in the browser by using the onunload or onbeforeunload events. This has been studied at great length by Patrick Hunlock, who uses the perhaps now common knowledge of using a Synchronous Ajax call to perform the page state save. Another use for the onbeforeunload [...]]]></description>
			<content:encoded><![CDATA[<p>Many people attempt a last ditch effort to save page state in the browser by using the onunload or onbeforeunload events.  This has been studied at great length by <a href="http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript">Patrick Hunlock</a>,  who uses the perhaps now common knowledge of using a Synchronous Ajax call to perform the page state save.</p>
<p>Another use for the onbeforeunload event to allow the user to cancel the action that initiated the user leaving in the first place.  Gmail uses this technique when the user is in the middle of writing a draft of an e-mail and attempts to leave the page.</p>
<p><a href='http://www.zachleat.com/web/wp-content/uploads/2008/04/gmail-confirm.png'><img src="http://www.zachleat.com/web/wp-content/uploads/2008/04/gmail-confirm.png" alt="" title="gmail-confirm" width="455" height="157" class="aligncenter size-full wp-image-129" /></a><br />
<em>Gmail&#8217;s pops up this prompt when the user attempts to leave the page while drafting an email.</em></p>
<p>Worthy to note, however, is that Opera <a href="http://www.quirksmode.org/bugreports/archives/2004/11/load_and_unload.html">doesn&#8217;t fire the unload event</a> when the browser refreshes the page, or uses the back/forward buttons to browse off of the page (I had no success with the fix posted in the comments on that page).  What&#8217;s worse, Opera never fires the onbeforeunload event.  This creates a serious problem with attempting to save page state prior to a user leaving your page.</p>
<p>Browser support aside, I believe that the onbeforeunload prompt is not an ideal way to protect the user from lost work (or unsaved page state).  Humanized has argued, and I agree, that <a href="http://www.alistapart.com/articles/neveruseawarning">an undo operation is much easier on the end user than a warning message</a>.  The strange thing is, Gmail could save the draft in a synchronous Ajax request in the onunload event.  They aren&#8217;t using the prompt to save Opera users from losing their drafts, since the Opera web browser doesn&#8217;t even fire the onbeforeunload event.  (Interestingly enough, they are using some sort of browser history management to fire a warning to the user when they press back, or forward, in Opera &#8212; but Reload can&#8217;t be caught using this method, so your could draft email be lost).</p>
<p>From a User Interface design standpoint, I would recommend just sticking with onunload.  You can still perform your synchronous Ajax call in the method to save the state of your page, so that the user can later resume their state or undo the operation. (Except for Back/Forward/Refresh in Opera, until they support a better onunload or any onbeforeunload).  The onbeforeunload prompt is an unnecessary evil, and doesn&#8217;t do much besides annoy the end user with another warning message and a mouse click.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2008/04/22/dont-let-the-door-hit-you-onunload-and-onbeforeunload/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Cross Domain XHR with Firefox 2</title>
		<link>http://www.zachleat.com/web/2007/08/30/cross-domain-xhr-with-firefox/</link>
		<comments>http://www.zachleat.com/web/2007/08/30/cross-domain-xhr-with-firefox/#comments</comments>
		<pubDate>Thu, 30 Aug 2007 23:40:20 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web Browsers]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Firefox]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/2007/08/30/cross-domain-xhr-with-firefox/</guid>
		<description><![CDATA[By now know you know that trying to do an XMLHttpRequest (XHR or AJAX) call to a domain that is different from the domain of the hosted JavaScript in Firefox will throw an exception. Error: uncaught exception: Permission denied to call method XMLHttpRequest.open If you don&#8217;t want a history of the past solutions, page down [...]]]></description>
			<content:encoded><![CDATA[<p>By now know you know that trying to do an XMLHttpRequest (XHR or AJAX) call to a domain that is different from the domain of the hosted JavaScript in Firefox will throw an exception.</p>
<p><code>Error: uncaught exception: Permission denied to call method XMLHttpRequest.open</code></p>
<p>If you don&#8217;t want a history of the past solutions, page down to see the final solution.</p>
<p>The web has solutions to this problem, but most of them involve changing your JavaScript code, which I thought to be less than ideal.  A common solution involves setting the UniversalBrowserRead security property in your JavaScript code [<a href="http://almaer.com/blog/archives/000794.html">Dion Almaer</a>, of Ajaxian fame]:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">netscape.<span style="color: #660066;">security</span>.<span style="color: #660066;">PrivilegeManager</span>.<span style="color: #660066;">enablePrivilege</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'UniversalBrowserRead'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The problem with that solution (obviously) lies in single browser proprietary JavaScript polluting your code.  And you have to set this property inside the scope of any usage (ie: inside your library file that does your AJAX calls and inside your callbacks, etc).</p>
<p>Why can&#8217;t it just be as easy as Internet Explorer?  They just pop-up a little security dialog asking you if you want to allow this access (which is also what the <code>enablePrivilege</code> function does as well).</p>
<p>Another solution involves setting the <code>capability.policy.default.XMLHttpRequest.open</code> preference inside your prefs.js Firefox preference file [<a href="http://blog.dirolf.com/2007/06/enabling-cross-domain-ajax-in-firefox.html">Mike Dirolf</a>].  This worked as desired and allowed the AJAX call, but anytime you attempt to access the resulting XML you received a nice exception as well.  It turns out this is the solution we wanted, it&#8217;s just incomplete.</p>
<h2><a name="solution">The Final Solution</a></h2>
<ol>
<li>Close Firefox.  It will overwrite your changes to the prefs.js file if you have it open.</li>
<li><em>Optional step</em>: This approach will open up your Firefox security quite a bit, so I&#8217;d recommend setting up a separate profile in Firefox to use when testing.  It will <strong>not </strong>pop up a security dialog when a cross-domain AJAX call is made.</li>
<li>Find your prefs.js file.  In Windows, it is typically located in the <code>C:\Documents and Settings\{YOUR_USERNAME}\ApplicationData\Mozilla\Firefox\Profiles\{YOUR_TEST_USER_PROFILE_ID}\prefs.js</code></li>
<li>Open it up and add the following lines:

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.XMLHttpRequest.open&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.CDATASection.nodeValue&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.Element.attributes&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.Element.childNodes&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.Element.firstChild&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.Element.getElementsByTagName&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.Element.tagName&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.HTMLCollection.length&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.HTMLCollection.item&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.Text.nodeValue&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.XMLDocument.documentElement&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.XMLDocument.getElementsByTagName&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.XMLHttpRequest.channel&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.XMLHttpRequest.open&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.XMLHttpRequest.responseText&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.XMLHttpRequest.responseXML&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.XMLHttpRequest.send&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user_pref<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;capability.policy.default.XMLHttpRequest.setRequestHeader&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;allAccess&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
</ol>
<p>This code was copied (with the exception of 1 line) from a source repository at [<a href="http://svn.kryogenix.org/filedetails.php?repname=kryogenix.org&#038;path=%2Fjackfield%2Ftrunk%2Fhtmlui%2Fprefs.js&#038;rev=0&#038;sc=0">kryogenix.org</a>]<br />
used in jackfield.  It wasn&#8217;t intended to be used for this purpose, but it works.</p>
<p>If you still get <code>Error: uncaught exception: Permission denied to call method _________</code> errors, you can add the method to your prefs.js.  I would appreciate a comment with any commonly used methods not included above.  Thanks.</p>
<p><strong>Update:</strong> Because this article is deprecated (applies to an older version of Firefox), I&#8217;m updating the blog title in the interest of minimizing the number of disappointed users.  Some might think this is a stupid thing to do, since it&#8217;s the most popular page on my blog, but I&#8217;m more interested in helping people than getting traffic.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2007/08/30/cross-domain-xhr-with-firefox/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Wash your mouth out with SOAP and the YUI Connection Manager</title>
		<link>http://www.zachleat.com/web/2007/05/09/wash-your-mouth-out-with-soap-and-the-yui-connection-manager/</link>
		<comments>http://www.zachleat.com/web/2007/05/09/wash-your-mouth-out-with-soap-and-the-yui-connection-manager/#comments</comments>
		<pubDate>Wed, 09 May 2007 22:56:58 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[SOAP]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/2007/05/09/wash-your-mouth-out-with-soap-and-the-yui-connection-manager/</guid>
		<description><![CDATA[Now you&#8217;ve done it. You watched an R rated movie while your parents weren&#8217;t looking and used some of your newfound acquired colorful language in front of them. Your mom goes for the Dial Liquid Soap, she&#8217;s going to wash that profanity right out of your dirty little mouth. Harsh? Maybe, but I couldn&#8217;t say [...]]]></description>
			<content:encoded><![CDATA[<p>Now you&#8217;ve done it.  You watched an R rated movie while your parents weren&#8217;t looking and used some of your newfound acquired colorful language in front of them.  Your mom goes for the Dial Liquid Soap, she&#8217;s going to wash that profanity right out of your dirty little mouth.</p>
<p>Harsh?  Maybe, but I couldn&#8217;t say the word &#8216;Bastards&#8217; without feeling Dial on my cleansed taste buds.  Maybe I should take legal action against the cinematic classic &#8216;<a href="http://www.imdb.com/title/tt0102059/">Hot Shots</a>.&#8217;</p>
<p>What are we here for?  Not liquid soap, perhaps some profanity.  But really, you want to use the SOAP web services protocol with your Yahoo User Interface application, in particular the Connection Manager component.  This tutorial assumes you are at least familiar with <a href="http://developer.yahoo.com/yui/connection/">the examples provided on the Yahoo! UI Library: Connection Manager website</a>.</p>
<p>How is SOAP different than any other AJAX call using the Connection Manager?  Well, normally, when you perform an HTTP Post AJAX call, it is passing in a key-value pair string encoded with the normal key=value&#038;key2=value format.  But when doing a SOAP call, we want to post an XML SOAP Envelope to the server instead of this key-value pair string.  In case you&#8217;re reading this article and don&#8217;t know what a SOAP Envelope looks like, I&#8217;ll post a sample here:<br />
<code><br />
&lt;s11:Envelope xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/"&gt;<br />
&lt;s11:Header&gt;<br />
&lt;/s11:Header&gt;<br />
&lt;s11:Body&gt;<br />
&lt;/s11:Body&gt;<br />
&lt;/s11:Envelope&gt;<br />
</code></p>
<p>Of course, it is beyond the scope of this article to argue whether a REST or a SOAP format for your Service Oriented Architecture is a better choice.  You&#8217;re stuck with SOAP, otherwise you wouldn&#8217;t have read this far.</p>
<p>So, let&#8217;s post our SOAP Envelope using the YUI Connection Manager with the code provided below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> targetUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://www.zachleat.com/postToServer.php'</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// this is not a real URL</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> callback <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> root <span style="color: #339933;">=</span> o.<span style="color: #660066;">responseXML</span>.<span style="color: #660066;">documentElement</span><span style="color: #339933;">;</span> 
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> root.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'faultstring'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// faultstring is a standard SOAP error message format</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> faultstring <span style="color: #339933;">=</span> root.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'faultstring'</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> detailed <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
			YAHOO.<span style="color: #660066;">UP</span>.<span style="color: #660066;">util</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span> root.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'detail'</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">childNodes</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> j<span style="color: #339933;">,</span> textNode <span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> textNode.<span style="color: #660066;">nodeValue</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> detailed <span style="color: #339933;">+=</span> textNode.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #006600; font-style: italic;">// do something with your error message stored in the faultstring variable, with a more detailed message in the detailed variable</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// this is an actual success.</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
	failure<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> o <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// do something with your failure.</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> message <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;s11:Envelope...YOUR SOAP MESSAGE HERE'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// we need to set our own header.</span>
YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Connect</span>._use_default_post_header <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Connect</span>.<span style="color: #660066;">initHeader</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'text/xml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> ajaxCall <span style="color: #339933;">=</span> YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Connect</span>.<span style="color: #660066;">asyncRequest</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span> targetUrl<span style="color: #339933;">,</span> callback<span style="color: #339933;">,</span> message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I hope this code helps some of you bastards.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2007/05/09/wash-your-mouth-out-with-soap-and-the-yui-connection-manager/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
