0

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
Walter
  • 173
  • 1
  • 13
  • The duplicate question has a brilliant explanation of the problem and several solutions. – Rory McCrossan Apr 03 '17 at 06:40
  • Thankyou. However I have tried those solutions and still have the same problem and I'm unable to see where my error is. Further help still wanted. – Walter Apr 03 '17 at 07:52
  • I spotted my mistake. the .done and .fail need to be on the ajax call itself unless I want to return a deferred object from my function. But fixing that doesn't solve anything as you cannot pass the value back from the .done either! – Walter Apr 03 '17 at 08:15
  • Also my callback function had a comma where a + should have been in the alert. I can now see the value but again it is useless to me as I cannot pass back the value. – Walter Apr 03 '17 at 08:17
  • Having now actually tested all the brilliant solutions I have determined that there is one major limitation. The return value can only be accessed after all calling methods have terminated. All solutions assume that the results is required as a final step (after all other code has terminated). If the value are required in the middle of an extensive method then there is no option but to design the method in two parts A & B with B being the callback event. Of course passing all the other required method variables between the two parts now becomes a problem as they no longer exist. – Walter Apr 04 '17 at 02:40

0 Answers0