因為寫了 jq4r,才有好心人幫忙 patch。

昨天把 jq4r 近期來的修改整理一下,釋出了 0.1.1 版,其中有一項最重要的改變,莫過於 jQuery 的 Ajax request 並不會在 header 中加入 Accept 這個參數,以致於在 Rails 中使用 respond_to 時會有問題,就像這段 code 說明的:

respond_to do |format|
 format.js { render :text => "this won't work with jquery" }
 format.html { render :text => "html" }
end

使用 jq4r 的網友 Jason Yates 提供了這樣的一段 patch:

--- jquery-1.2.1.js	2007-11-12 21:17:21.000000000 -0500
+++ jquery.js	2007-11-12 21:17:57.000000000 -0500
@@ -2292,6 +2292,12 @@
 		// Open the socket
 		xml.open(s.type, s.url, s.async);
 
+    if( s.dataType == 'json' ) {
+      xml.setRequestHeader("Accept", "application/json, text/javascript, text/html, application/xml, text/xml, */*");
+    } else {
+      xml.setRequestHeader("Accept", "text/javascript, text/html, application/xml, text/xml, */*");
+    }    
+
 		// Set the correct header, if data is being sent
 		if ( s.data )
 			xml.setRequestHeader("Content-Type", s.contentType);

給大家參考看看囉。