很抱歉重复,但我可以在其他帖子中找到任何解决方案。
我正在尝试使用对Web服务的Ajax调用来填充javascript对象。我不了解其逻辑,也不清楚为什么在调用后没有设置我的对象。我写了这个小例子来解释我的问题:
代码 :
function Cellule(){
this.test = 0;
this.textfunction();
}
Cellule.prototype.textfunction = function(){
this.test = 1;
$.ajax({
type: "GET",
url: "BASEURL/cellules",
data: "",
success: function(msg){
console.log("success");
this.test = 2;
console.log(cellule);
}
});
};
var cellule = new Cellule();
控制台输出:
success
Cellule {test: 1}
this
不指cellule
该bind()
方法创建一个新函数,该函数在调用时将其关键字设置为提供的值
或指定context: this
将使用cellule
成功处理程序的上下文。
试试这个:
function Cellule() {
this.test = 0;
this.textfunction();
}
Cellule.prototype.textfunction = function() {
this.test = 1;
$.ajax({
type: "GET",
url: "../slimrest/andon/cellules",
data: "",
success: function(msg) {
console.log("success");
this.test = 2;
console.log(cellule);
}.bind(this)
});
};
var cellule = new Cellule();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句