How do I get a value from my Ajax request ?
I have read about how to get a response from an Ajax request but I just can't seem to get it. I try to work with the value before it's back from the server so I get an undefined value.
In my page I create a
vacationDayRequested object and assign it to a variable called vdr.
- var vdr = new vacationDayRequested(requestedDate, clockNo);
- alert(vdr.Selectable)
My
vacationDayRequested object is defined like this:
- function vacationDayRequested(_requestedDate,_clockNo) {
- this.status = "unknowm";
- this.ClockNo = _clockNo;
- this.DayRequested = _requestedDate;
- this.Selectable = IsSelectableBASIC(this);
- }
I get the value of the Selectable property by calling a function and going up to the server:
- function IsSelectableBASIC(vacationDayRequested)
- {
- var returnValue = false;
-
- $.ajax({
- url: "/VacationRequest/IsDaySelectableBASIC",
- type: 'POST',
- contentType: 'application/json; charset=utf-8',
- data: JSON.stringify(vacationDayRequested)
-
- }).done(function (data) {
- return data.OperationMessage;
- });
- }
I know what's happening, but I do not know how to fix the issue. I am geting an undefined on
alert(vdr.Selectable). I thought by returning the value from done() would fix my problem. How do I refactor the code to work with the value I need from the server without changing async to false in the ajax request?