tusbasaのブログ

業務や勉強中に調べたことを自分用にメモするブログ

【JQuery】ajax context

ajaxのコールバック関数の中(then以下)で普通にthisを使うと戻り値が使われてしまう。
contextオプションを使うとコールバック関数の中で使うthisに任意の値を設定できる。

配列やハッシュでも設定できる。this.[キー名]でバリューを参照できる。

$.ajax({
          url: ,
          type: ,
          dataType: "",
          data: ,
          context: {
            A: this,
            co_ellipsis: X }
      }).then(

this.A
this.co_ellipsis

参考 http://kihon-no-ki.com/jquery-ajax-pass-value-to-callback-use-context https://www.flatflag.nir87.com/this-1371#ajax_done