I built the following object:
var cls = {
offset : 0,
onSuccess : function(data) {
alert(this.offset);
}
};
$(document).ready(function(){
$("form").submit(function() {
$.post(
"/ajax",
{},
cls.onSuccess
);
});
});
When the ajax returns and onSuccess of cls is called then I get an alert of undefined.
If I just call cls.onSuccess() then the alert returns 0 ex expected. Any reason for this behavior?
You need to give the AJAX function a context for the callback. I would use .ajax() instead of .post() and pass cls as the context: