0

I have an ASP.NET form that calls a web service using ASP.NET AJAX. How do I make the call synchronous?

// test.aspx

<script type="text/javascript">
    function GetDescription(){
        var code = document.getElementById("<%=txtCode.ClientID%>").value;
        var description = 
            MyNamespace.MyService.GetDescription(
                code, onSuccessGetDescription, null, null);
        return false;
    }

    function OnSuccessGetDescription(result){
        var txtDescription = 
            document.getElementById("<%=txtDescription.ClientID%>");
    }
</script>
<script type="text/javascript">
    $(document).ready(//function () {

        $('#<%=cmdSave.ClientID%>').click(function (e) {
            e.preventDefault();
            if (Page_ClientValidate()){
                // Populate jQuery.UI dialog box with code and description
                // and then
                $("#confirm-dialog").dialog("open");
            }
        });

    }
</script>

<asp:TextBox ID="txtCode" />
<asp:TextBox ID="txtDescription" />
<asp:Button ID="cmdSave" />

// test.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    txtCode.Attributes["onblur"] = "GetDescription();";
}

The answers to this question that I have seen say that I should set the async attribute to false, but I don't see how to do that here. ASP.Net Ajax - PageMethods Synchronous call and retrieval of results says that it can't be done. I am trying to get a second opinion.

If I can't make the call synchronous, would it be legal to make GetTransaction poll until the OnSuccess function returns? What would be an efficient (i.e. not CPU-intensive) way to do that?

Community
  • 1
  • 1
Test User
  • 37
  • 1
  • 9

1 Answers1

0

JQuery Asynchronous

jQuery.ajax({
url: 'http://example.com/catalog/create/' + targetNode.id + '?name=' + encode(to.inp[0].value),
success: function (result) {
   if (result.isOk == false) alert(result.message);
    },
    async: false
});
Community
  • 1
  • 1
Tony Dong
  • 3,213
  • 1
  • 29
  • 32
  • I am almost there. When calling a web service method this way, are you expected to parse the result yourself? The responseText is something like: Pepsi 2L – Test User Sep 03 '16 at 13:09
  • You can convert on web service site into JSON format. http://blogs.microsoft.co.il/pini_dayan/2009/03/12/convert-objects-to-json-in-c-using-javascriptserializer/ System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); string sJSON = oSerializer.Serialize(object); – Tony Dong Sep 06 '16 at 17:19