简体   繁体   中英

How to call a method in ajax callback function?

I am trying to call a method in the callback function.

jsTest.prototype.getTitle = function() {
   thisJS=this

   //the console will returns my object...so it's not undefined.
   console.log(thisJS);


   //codes.....


   //ajax callback function
    ajaxcall.callback=function(data){

        //call the addName method
        thisJS.addName(data, 1);

    };

}


jsTest.prototype.addName=function(data, bool){
    console.log(data);
}

I got an error saying

Uncaught TypeError: Cannot call method 'addName' of undefined 

Are there any ways to solve this? Thanks so much!

try changing

thisJS=this

to this:

var thisJS=this

to call the method addName, you have to make a reference of that class like in OOP..

if you dont have a jsTest object created then you cant use addName

var item = new jsTest();
item.addName(data, 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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