ariyasacca

2004|08|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|11|12|
2006|01|02|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|08|09|10|11|12|
2009|01|02|03|04|05|06|07|08|09|10|11|12|
2010|01|02|03|04|05|06|07|08|09|10|11|12|
2011|01|02|03|04|05|06|07|08|09|10|11|12|
2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|03|04|05|06|07|08|09|10|11|12|
2025|01|02|03|04|05|06|07|08|09|10|

2010年01月18日 (月) [長年日記]

[雑記]jQuery 1.4でjQuery.param()の変更点に気を付けないとjQuery.ajax()が動かなくなることがある

jQuery 1.4がリリースされましたが、jQuery.param()の変更点が割とドラスティックで、少しハマってしまったのでメモしておきます。

オブジェクトからクエリストリングを作る場面の動作が、デフォルトでRailsライクなシリアライズ(Redmineのチケット一覧を絞り込む時のパラメータの様に、ネストしたオブジェクトや配列を展開)をするようになりました。

どうもオブジェクトのプロパティを列挙してFunction型だった時に、windowスコープで実行結果を取りに行くっぽいです。

jQuery.param()の第2引数traditionalにtrueを指定すると、これまで通りの動作をします。

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.js"></script>
<script type="text/javascript">
 var MySize = function(x, y) {
 this.x = x;
 this.y = y;
 }
 MySize.prototype.toString = function() {
 return String(this.x) + "," + String(this.y);
 };
 var s = new MySize(50, 50);
 var params = {
 "size": s
 };
 // size[x]=50&size[y]=50&size[toString]=undefined,undefined
 console.log(decodeURIComponent($.param(params)));
 // size=50,50
 console.log(decodeURIComponent($.param(params, true)));
</script>

undefinedになる程度ならまだ良いのですが(良くないけど)、以下のような関数を定義していると、実行時エラーになってしまいます。

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.js"></script>
<script type="text/javascript">
 var MySize = function(x, y) {
 this.x = x;
 this.y = y;
 }
 MySize.prototype.toString = function() {
 return String(this.x) + "," + String(this.y);
 };
 MySize.prototype.isWide = function() {
 return this.x > this.y;
 };
 MySize.prototype.isNarrow = function() {
 return this.x < this.y;
 };
 MySize.prototype.isSquare = function() {
 return !this.isWide() && !this.isNarrow();
 };
 var s = new MySize(50, 50);
 // false
 console.log(s.isWide());
 // false
 console.log(s.isNarrow());
 // true
 console.log(s.isSquare());
 var params = {
 "size": s
 };
 // s.isSquare.call(window); => this.isWide is not a function
 console.log(decodeURIComponent($.param(params)));
 // size=50,50
 console.log(decodeURIComponent($.param(params, true)));
</script> 

jQuery 1.4にアップデートした時に、今まで動いていたjQuery.ajax()を利用している箇所が突然動かなくなった場合は、オプション引数のtraditionalにtrueを渡したり、またはjQuery全体のajaxSettingsを変更してしまうのが良いようです。

// ajax()のtraditionalにtrueを指定
$.ajax({
 "url": "http://www.example.com/", 
 "data": params, 
 "success": onSuccess, 
 "traditional": true
});
// jQuery全体の動作設定を変更
$.ajaxSettings.traditional = true;
[ツッコミを入れる]

最近のツッコミ

  1. 雷悶 (2025年10月22日(水)20:46)「>週刊中年ジャンプ コメントありがとうございます! 老眼に負けずワンピ完結まで読むぞ!!」
  2. 週刊中年ジャンプ (2025年10月22日(水)19:54)「自分もはてブから来ました!雷悶さん、お世話になりました!今後ともよろしくお願いします!!!このインターネット感素晴ら..」
  3. 雷悶 (2025年10月22日(水)10:40)「>同時期にYJを通過した者です 「第二の創業」って宣言する社長さん世の中に沢山いるけど人事制度いじる程度だろな〜っ..」

参号館 の中の 日記(ariyasacca)

トップ «前の日記(2010年01月17日 (日)) 最新 次の日記(2010年01月22日 (金))» 編集
Generated by tDiary version 5.2.3
Powered by Ruby version 3.0.2-p107

AltStyle によって変換されたページ (->オリジナル) /