类的jQuery ajax回调成员?

Pao*_*olo 2 javascript jquery

可能重复:
有没有办法让JQuery ajax成功函数访问它所包含的对象?

我有一些类似的代码

myClass.prototype.doStuff = function(){

  $.ajax({
        type: 'POST',
        url: $('#form').attr('action'),
        data: $('#form').serialize(),
        success: this.callback
  });
};

myClass.prototype.callback = function(data){
   if(this.someFlag){
     //do some stuff  
   }

};
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我应该this是myClass的实例,但实际上并非如此.这是为什么?

Mat*_*all 7

context: this作为ajax选项传递.

此对象将成为所有与Ajax相关的回调的上下文.默认情况下,上下文是一个对象,表示调用中使用的ajax设置($.ajaxSettings与传递给的设置合并$.ajax).

$.ajax({
    type: 'POST',
    url: $('#form').attr('action'),
    data: $('#form').serialize(),
    context: this,
    success: this.callback
});
Run Code Online (Sandbox Code Playgroud)