ariyasacca

カテゴリ一覧

Biz | SF | Software | tDiary | Web | ゲーム | サバティカル | スポーツ | ミステリ | メタル | 健康 | 投資 | 携帯 | 時事ネタ | 死生観 | 資格 | 雑記
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|

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. 雷悶 (2023-06-24(土)22:25)「新大阪駅で降りると必ず視界に入るサムティ行くしかないのか~?」
  2. ブリネル (2023-06-24(土)20:58)「次はサムティアンド箕面ビールツアーしかないっしょ〜 DIE WITH ZERO〜」
  3. 雷悶 (2023-03-08(水)19:02)「10年後にはリゾマンを購入している筈っしょ~(言うだけ)」

参号館  の中の  日記(ariyasacca)

トップ «前の日記(2010-01-17 (日)) 最新 次の日記(2010-01-22 (金))» 編集