Thankyou for providing those pointers but I have read them and tried implementing Felix Kings How to implement... but that is why I'm asking for more help. I can't seem to get any results back except in a simple success event using alert.
As you can see in the success code I have both an alert and a callback called and only the alert gets the return value!!! Why?
I have also amended the code to also use .done & .fail deferred objects but they don't appear to do anything at all!!! I must have something wrong in them but can't see what it is!
I understand the event call backs are async but I still cannot get any response data back except in a basic alert.
If I want to use the return data on the form then everything works ok because all events async or otherwise are completed before the form is reloaded. However if I want to use the value in the js immediately then I'm not sure what to do if I want the data and want to avoid making it synchronous.
Here is my code sample:
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;
}
Displays the following alerts:
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