我有一个我实例化的javascript对象名为vacationDayRequested。
function vacationDayRequested(_requestedDate,_clockNo) {
this.status = "unknowm";
this.ClockNo = _clockNo;
this.DayRequested = _requestedDate;
this.Selectable = IsSelectableBASIC(this);
}
我想通过名为IsSelectableBASIC的ajax函数分配Selectable属性的值:
function IsSelectableBASIC(vacationDayRequested)
{
var returnValue = false;
$.ajax({
url: "VacationRequest/IsDaySelectableBasic",
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(vacationDayRequested),
success: function (data) {
if (data.OperationMessage == "yes") {
returnValue = true;
}
else {
returnValue = false;
}
}
});
return returnValue;
}
我使用Chrome中的开发人员工具检查了我的ajax返回的JSON,其OperationMessage值为"是"然而我的假期请求。可选择回来了"假"。我的javascript中是否存在设计缺陷?
以下是我实例化对象并在客户端代码中使用它的方法:
var vdr = new vacationDayRequested(requestedDate, clockNo);
alert(vdr.Selectable);
更新:9/11/2014
现在,回调变得越来越清晰。在描述下面的代码中发生了什么时,我是否正确?另外,如果我在整个过程中正确使用术语回调,请告诉我。
GetValueOfX执行并具有ajax函数,其中成功调用DoSomething,因为我们传入了DoSomething。
按
$("#btn")。click(function(){ GetValueOfX(DoSomething);
});
function DoSomething(result)
{
***//This is the correct place for code that depends on the ajax return***
x = result;
debugger;
alert(x);
}
function GetValueOfX(callback)
{
$.ajax({
url: "/JSTesting/GetSomeTestData",
type: 'POST',
contentType: 'application/json; charset=utf-8',
success: callback
})
***//if we had code here it might fire before the ajax request is complete***
}