How to pass context in jQuery ajax callback functions

var Box = function(){ this.parm = {name:"rajakvk",year:2010}; Box.prototype.jspCall = function() { $.ajax({ type: "post", url: "some url", success: this.exeSuccess, error: this.exeError, complete: this.exeComplete }); } this.exeSuccess = function(){ alert(this.parm.name); } } 

I do not get the Box object inside the exeSuccess method. How to pass Box object inside exeSuccess method?

+47
javascript jquery
Oct 05 '10 at 12:20
source share
1 answer

Use the context parameter, for example:

  $.ajax({ context: this, type: "post", url: "some url", success: this.exeSuccess, error: this.exeError, complete: this.exeComplete }); 

The context parameter determines which context callback is called from ... so it determines that this refers to this function.

+76
Oct 05 2018-10-10
source share



All Articles