I’m attempting to create an ajax function that returns a specific string from a page, and then sets it in the class I created. However, when I attempt to call this.setGlideUser within the ajax success parameter, it tells me that the
object has no function setUserGlide.
And when I attempt to call this.setUserGlide outside of the success parameter, and then look at the value of _glideUser, _glideUser is undefined. Here is a snippet of my code.
function main()
{
this.start = true;
}
main.prototype.load = function()
{
var _glideUser;
$.ajax(
{
url: '/home',
dataType: 'text',
success: function(data)
{
// Truncated
_glideUser = unescape(data).match(pattern);
}
});
this.setGlideUser(_glideUser);
return 1;
}
main.prototype.setGlideUser(user)
{
return this._glideUser = user;
}
main.prototype.getGlideUser()
{
return this._glideUser;
}
var _main = new main();
_main.load();
// check that the ajax has completely loaded
_main.getGlideUser(); // returns undefined
I can’t think of a way to set the _glideUser variable within the main class from the Ajax success function. I would like to get the value of _glideUser within another function, to do things with it. Any suggestions?
There is no
this._glideUser, because_glideUseris local to yourloadfunction. Instead of declaringvar _glideUserinsideload, set it as a public property from the constructor:(Your first call to
setGlideUserwould actually create such a property, but isn’t it confusing to have both?)Also, as others mentioned, you’re trying to
setGlideUserbefore your ajax operation completed. You need to do that from thesuccesscallback: