In order to assign it to the object initiating the Ajax call, you can utilize an arrow function to grasp the contextual this
class MyCaller {
response: any;
doCall() {
$.ajax({
method: "POST",
url: 'URL',
data: { email:'<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="582c3d2b2c183f35393134763b373535">[email protected]</a>', password:'pass'}
}).done((msg ) => {
// this indicates MyCaller, whereas a basic function would not
this.response=msg;
}).fail((msg) => {
this.response=msg;
})
}
}
If you are working with TypeScript, you have the option to utilize async and await to streamline your code. The following would be the equivalent:
class MyCaller {
response: any;
async doCall() {
try {
this.response = await $.ajax({
method: "POST",
url: 'URL',
data: { email:'<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="532736202713343e323a3f7d303c3c">[email protected]</a>', password:'pass'}
})
}catch(error) {
this.response = error
}
}
}