<?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>Kej.tw Revisited &#187; jquery</title>
	<atom:link href="http://blog.kej.tw/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.kej.tw</link>
	<description>一個懶鬼的部落格</description>
	<lastBuildDate>Wed, 27 Apr 2022 20:02:14 +0000</lastBuildDate>
	<language>zh-TW</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8.3</generator>
	<item>
		<title>jQuery 1.0.2</title>
		<link>http://blog.kej.tw/2006-10/12/jquery-102/</link>
		<comments>http://blog.kej.tw/2006-10/12/jquery-102/#comments</comments>
		<pubDate>Thu, 12 Oct 2006 14:37:27 +0000</pubDate>
		<dc:creator><![CDATA[Kej]]></dc:creator>
				<category><![CDATA[程式設計]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://blog.kej.tw/2006-10/12/jquery-102/</guid>
		<description><![CDATA[手上的某個專案因為歷史的關係:?:，所以先後分別用了 prototype 跟 jQuery 兩個 JavaSc [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>手上的某個專案因為歷史的關係:?:，所以先後分別用了 <a href="http://prototype.conio.net/">prototype</a> 跟 <a href="http://jquery.com/">jQuery</a> 兩個 JavaScript library，今天想了一下，還是決定統一用一個 library 就好了；由於 jQuery 看起來較輕薄短小（其實 <a href="http://dean.edwards.name/packer/">pack</a> 起來後兩個差不多大），加上 Chainable、支援 XPath 等一些像我這種懶人喜愛的特性的緣故，決定全部使用 jQuery。</p>
<p>程式都改完、測試好之後才想到該去 <a href="http://jquery.com/">jQuery 官網</a>看看，發現 10/9 剛發佈了新的 1.0.2 版，於是就下載回來更新（之前是用 1.0.1 版），沒想到一更新之後所有 $.ajax callback 的部份全都失效了，查了一下 <a href="http://jquery.com/api/">API</a> 發現原來在 <a href="http://jquery.com/api/#$.ajax">$.ajax</a> 的地方是這樣寫的：</p>
<blockquote><p>(Function) error &#8211; A function to be called if the request fails. The function gets passed two arguments: The <strong>XMLHttpRequest object</strong> and a string describing the type of error that occurred.</p>
<p>(Function) success &#8211; A function to be called if the request succeeds. The function gets passed one argument: The <strong style="color:#f00;">data returned from the server</strong>, formatted according to the &#8216;dataType&#8217; parameter.</p>
<p>(Function) complete &#8211; A function to be called when the request finishes. The function gets passed two arguments: The <strong>XMLHttpRequest object</strong> and a string describing the type the success of the request.</p></blockquote>
<p>而我的程式在 success 時的 callback function 卻也是以回傳一個 XMLHttpRequest 物件的方式來寫的，因此當事實上回傳的已經是一個字串時，也就不會有 responseText 囉（因為回傳值本身就是responseText），然後 callback function 就理所當然的死掉了。</p>
<p>當我再把 1.0.1 版蓋回去測試時，卻發現程式跑起來一切正常，一丁點錯誤訊息也沒有；原來 1.0.1 版 success 的部份的確是回傳一個 XMLHttpRequest 物件，所以或許是在 1.0.2 版有作更改了吧，不過我怎樣也找不到哪邊有 change log 提到這點，也找不到舊版的 API 文件來作對照，但是經過一連串的交叉測試之後應該可以確定是這樣子沒錯。所以這下子有兩個選擇：一個是把 success 改成 complete，這樣就不用去更動 callback function，另一個是將 callback function 內的 XMLHttpRequestObject.responseText 改成直接用 responseText，亦即將</p>
<blockquote><p><code>function successCallBack(XMLHttpRequestObject){<br />
<span style="margin-left:40px;">eval("var jsonVars = " + XMLHttpRequestObject.responseText);</span><br />
<span style="margin-left:40px;">………</span><br />
}</code></p></blockquote>
<p>改成</p>
<blockquote><p><code>function successCallBack(responseText){<br />
<span style="margin-left:40px;">eval("var jsonVars = " + responseText);</span><br />
<span style="margin-left:40px;">………</span><br />
}</code></p></blockquote>
<p>而我選的是後者。</p>
<p>喔，其實還有另一個選擇，就是用舊版 library，但，何必咧？</p>
<p>真不知道為什麼要將 success 的部份設計的跟另外兩個狀態不一樣啊……</p>
<p><strong>2006-10-14 update :</strong><br />
<a href="http://jquery.com/blog/">jQuery 的官方部落格</a>今天<a href="http://jquery.com/blog/2006/10/13/minor-api-change-in-102/">提到了這點</a>：其實這項 API 的變動，原本是預計等到 1.1 版的 major release 時才會變更的，結果「不小心」在 1.0.2 時就先用了，然後又順便忘了提到這點……</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kej.tw/2006-10/12/jquery-102/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
