简体   繁体   English

jQuery $ .ajax获取响应与将此对象嵌套传递给成功回调函数冲突

[英]Jquery $.ajax Get response conflicting with nested passing of 'this' object to 'success' callback function

I am trying to do a jquery $.ajax Get inside a method of a class. 我试图做一个jQuery $.ajax Get类的方法内。 The data supplied by the Get request to be used in the callback function on success and also the class needs to be modified inside the same callback. Get请求提供的数据将在成功使用时在回调函数中使用,并且该类也需要在同一回调中进行修改。 So, I had to .apply the callback on the this identifier of the original class since otherwise ,inside the callback, this was getting replaced locally. 所以,我不得不.apply回调在this标识的原始类的,因为否则,回调里面, this是越来越局部更换。 But while applying callback to this the data returned by Get is coming as undefined . 但是在this应用回调时, Get返回的数据为undefined How do I work around this problem. 我如何解决此问题。 Any help appreciated. 任何帮助表示赞赏。

var tableblock = function(tablename){
     this.tablename = tablename;
     this.numusers = 0;
     this.userpool = new Array();

     this.addme = function(){
        bodyContent = $.ajax({
            type: "GET",
            url: "controllers/bestpals.php",
            data: "addpal=1&palname="+userid+"&nocache="+Math.floor((Math.random()*100)+1),
            error: function(xhr, statusText){alert('could not add pal. refresh page.');},
            success: function(msg){
                alert(msg); // this will give undefined when .apply(this) 
                myid = msg;
                syncpals(this,true);
            }.apply(this) // using this alert(msg) above gives undefined 
        });
     };
 }

You can provide a context parameter in your ajax configuration; 您可以在ajax配置中提供context参数。 all ajax callbacks will be called in that context (ie, this inside the callback will refer to the value you pass as context in your $.ajax() call): 所有的Ajax回调将在上下文中调用(即this回调中会参考你传递的价值context$.ajax()调用):

SomeClass.prototype.methodName = function() {
    $.ajax({
        ....
        context: this    // callbacks will be called with the same context that methodName() is running in
        ....
    });
}

You want .bind(this) , not .apply(this) . 您需要.bind(this) ,而不是.apply(this) bind changes the this pointer of a function, while apply actually runs the function in the new context. bind更改函数的this指针,而apply实际上在新上下文中运行该函数。

I think you meant bind not apply . 我觉得你的意思bindapply bind creates a new function that calls the wrapped one whilst setting this ; bind创建一个新函数,在设置this函数时调用包装的函数; apply calls the function straight away as soon as addme() runs. applyaddme()运行后立即调用该函数。

However bind is an ECMAScript Fifth Edition feature, so you'd need to do one of: 但是bind是ECMAScript第五版的功能,因此您需要执行以下一项操作:

  1. polyfill bind for older browsers; 适用于较旧浏览器的polyfill bind
  2. use the jQuery equivalent proxy ; 使用jQuery等效proxy ;
  3. use the context argument to ajax , which sets this explicitly; 使用context参数ajax ,它设置this明确;
  4. remember var that= this; 记住var that= this; in addme and use the enclosed that reference in the inner function. addme并在内部函数中使用附带的that引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM