我试图在$.ajax Get类的方法中做一个jquery .由Get成功回调函数中使用的请求提供的数据以及类需要在同一回调内修改.所以,我不得不对原始类.apply的this标识符进行回调,因为否则,在回调中,this它将在本地被替换.但是,将回调应用于this返回的数据Get即将发布undefined.我该如何解决这个问题.任何帮助赞赏.
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
});
};
}
Run Code Online (Sandbox Code Playgroud)