我一直在尝试从其中一个方法内的 AJAX 调用访问同一类的一些方法,但它不起作用。这里可能有什么问题?我收到此错误 Uncaught TypeError: this.createTimeline is not a function
请参阅下面代码的注释!
//sequenceRender is the class and below is one method
sequenceRender.prototype.ajaxSequence = function(){
this.ajaxSequence = $.ajax('getSequence.php', {
dataType: 'json',
timeout: 2000
});
this.ajaxSequence.done(function (data, status, jqXhr) {
console.log(data)
this.SEQUENCE=data // I cannot access properties
this.createTimeline() // or methods from same class
this.createWells() // from inside here
})
this.ajaxSequence.fail(function (jqXhr, textStatus, errorMessage) {
console.log(errorMessage)
})
}
最佳答案
当您尝试在事件中调用此函数时,this
看起来将具有不同的值
尝试添加以下更改,并告诉我这是否适合您
sequenceRender.prototype.ajaxSequence = function() {
mainContext = this;
this.ajaxSequence = $.ajax('getSequence.php', {
dataType: 'json',
timeout: 2000
});
this.ajaxSequence.done(function (data, status, jqXhr) {
console.log(data)
mainContext.SEQUENCE=data
mainContext.createTimeline() // call the 'this' from here
mainContext.createWells()
})
this.ajaxSequence.fail(function (jqXhr, textStatus, errorMessage) {
console.log(errorMessage)
})
}
关于javascript - 调用 jQuery AJAX 中包含的方法,该方法包含在同一类的方法中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59754788/