0

I have a java script function which returns value of status. My function is as follow :

function IsReportAlreadyExists() {
            var result=true;
            $.ajax({
                type: "POST",
                url: "ReportDetail.aspx/CheckReport",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    if (msg.d != "") {
                       result= confirm(msg.d);
                    }
                },
                failure: function (response) {
                    alert(response.d);
                },
                error: function (response) {
                    alert(response.d);
                }
            });
            return result;
        }

I am calling this function on client click event of a asp.net button control.

 <asp:Button ID="btnSave" Style="margin-right: 10px" runat="server" CausesValidation="false"
                                                    OnClick="btnSave_Click" Text="Save" Width="70px" OnClientClick="return IsReportAlreadyExists();" />

On client click of button javascript function always return a true value.I mean i am not getting the value of result which is set by confirm(msg.d). Is there any method to achieve this ? Please help me guys. Your help will be really appreciated.

Thanks,Rajbir

Rajbir Singh
  • 1,641
  • 6
  • 23
  • 46
  • 1
    Duplicate of [How to return AJAX response Text?](http://stackoverflow.com/questions/1225667/how-to-return-ajax-response-text) and *many, many* others. – Quentin Mar 07 '13 at 11:00

1 Answers1

1

It because the code is asynchronous. First it return result=true and then after ajax call is finished the result is changed. You need to process your result in success callback.

jcubic
  • 61,973
  • 54
  • 229
  • 402
  • Could you please give me example. Actually i have not use the callback functions before. – Rajbir Singh Mar 07 '13 at 12:18
  • @RajbirSingh in your code `success: function (msg) { if (msg.d != "") { result= confirm(msg.d); } }` is a callback function – jcubic Mar 07 '13 at 15:05