Ajax Class error with accessing class variable in onreadystatechange function

后端 未结 1 1410
攒了一身酷
攒了一身酷 2021-01-26 13:11

I am currently writing a JavaScript Ajax class and have encountered an error. In the function processRawData() I can\'t seem to access the class variable xhr by using this.xhr.

相关标签:
1条回答
  • 2021-01-26 13:51

    Looks like your problem might be because in processRawData() you are returning another function and referencing this.xhr.readyState, but 'this' now references the returning function and not the Ajax class. Try:

    Ajax.prototype.processRawData = function(dataType, callback){
    
    var that = this; //'that' references the current Ajax instance
    
     return function()
     {
        if (that.xhr.readyState === 4 && that.xhr.status === 200) {...
    
    0 讨论(0)
提交回复
热议问题