谢谢你提供这些指示,但我已经阅读过并尝试实施Felix Kings如何实施......但这就是我要求更多帮助的原因。除了使用警报的简单成功事件外,我似乎无法获得任何结果。
正如您在成功代码中看到的那样,我既有警报又有回调,只有警报才能获得返回值!为什么呢?
我还修改了代码以使用.done& .fail延期对象,但他们似乎根本没有做任何事情!我必须在他们身上出错,但不能看到它是什么!
我理解事件回调是异步的,但除了基本警报外,我仍然无法获得任何响应数据。
如果我想在表单上使用返回数据,那么一切正常,因为在重新加载表单之前完成所有异步或其他事件。但是,如果我想立即使用js中的值,那么如果我想要数据并希望避免使其同步,我不知道该怎么做。
这是我的代码示例:
main...
alert("Test Verify: " + VerifyEntry(e));
function VerifyEntry(DT) //wrapper for async callback
{
var result = "not initialised";
$this = $(this);
alert("VerifyEntry data 1>" + DT);
VerifyEntrySub
(
DT,
function (response) { $this.result = response; alert("Callback: ", response);}
).done
(
function(response) { $this.result = response; alert("Done: " + response); }
).fail( function(response) { alert("Fail") ; } );
alert("VerifyEntry data 2>" + result);
return result;
}
function VerifyEntrySub(DT, callback)
{
var result = 0;
alert("VerifyEntrySub data 1>" + DT);
if (TskID != "") {
if (jQuery) {
jQuery.ajax({
url: '../Forms.asmx/RecExists',
type: "POST",
data: '{ postdata: "' + DT + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
beforeSend: function () { /*alert("Sending data string >" + CurrFrmData);*/ },
success: function(response){ alert("Alert: " + response.d); callback(response.d); },
failure: function (response) { alert(response.d); }
});
}
result = 0;
} else {
result = -1;
}
alert("VerifyEntrySub data 2>" + result);
return result;
}
显示以下警告:
VerifyEntry data 1> Some value // this is correct
VerifyEntrySub data 1> Some value // this is correct
VerifyEntrySub data 2> 0 // this is correct
VerifyEntry data 2> not initialised // this is not initialised? Not getting callback value!
Test Verify: not initialised // this is not initialised? Not getting callback value!
Alert: 1 // Correct one record found.
Callback: // this is blank. Should also be 1 record found! And Callback occurs out of sync (async model)
// Done: never entered
// Fail: never entered