I have this class with this ajax call:
Person = function () {
this.__type = "PersonDto:#Empower.Service.Common.Dto"; this.Name = undefined; this.Surname = undefined; this.GetById = function (id) { return $.ajax({ type: "POST", url: "/Services/PersonService.svc/GetPersonById", data: JSON.stringify(id), contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { ... this = data; ... } }); } };
In success of ajax call i want to set the current instance of person, but “this” is not scope correct for setting.
There is a more elegant way than using a global variable?
Thank you in advance for your help, and I apologize for my bad English
The
jQuery.ajax()[docs] method gives you acontextproperty where you can set the value ofthisin the callbacks.Just do:
…in your call, as in: