The meaning of this depends on how each function is called. I see 3 ways to solve this problem:
1. Create an alias for this
var that = this; this.getArea = function () { $.ajax({ url: base_url +'spotlight/aGetArea', type: 'POST', success: function (data) { that.area = data; } }); };
2. Using the jQuery .ajax context option
this.getArea = function () { $.ajax({ url: base_url +'spotlight/aGetArea', type: 'POST', context : this, success: function (data) { this.area = data; } }); };
3. Using a related function as a callback
this.getAreaSuccess = function (data) { this.area = data; }; this.getArea = function () { $.ajax({ url: base_url +'spotlight/aGetArea', type: 'POST', success: this.getAreaSuccess.bind(this) }); };
source share