Gopinath Koothaperumal
Gopinath Koothaperumal

Reputation: 398

Asynchronous processing using AJAX and .net Web Service

I'm stuck with this problem for quite some time. Advise needed...

Alright i have this ASPX test-service page(AJAX)

 <script type="text/javascript">
    $(document).ready(function () {      
        $('#btnTest').click(function () {
            $('#btnTest').hide();
            $.ajax({
                type: 'POST',
                url: "request.asmx/newSync",
                data: '',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                complete: function () {
                    $('#btnTest').show();
                },
                failure: function (msg) {
                    $('#result').text(msg);
                    //alert(msg);
                }
            });
        });
    });
</script>

The Web Service

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[ScriptService]
public class test_request : System.Web.Services.WebService
{

public test_request () {


}

[WebMethod(EnableSession = true)]
public SyncAsync newSync()
{
    var response = new SyncAsync();

    //Test Processing
    System.Threading.Thread.Sleep(5000);

    try
    {
        response.Status = "Done";
        return response;
    }
    catch (Exception ex)
    {
        response.Status = "error";
        response.ErrorMessage = ex.Message;
        ErrorSignal.FromCurrentContext().Raise(ex);
    }

    return response;
}

}

Okay my Question is this...Is this Asynchronous?...Meaning when i click the button it sends the request and i shall not wait for any response back from the webservice...?

Can someone explain?

Thank you

Upvotes: 0

Views: 2684

Answers (3)

fly_ua
fly_ua

Reputation: 1054

In my understanding we need to define Asynchronous what? Request or Web Service

to make async call from jquery you need to set ajax settings. Async is true by default.

to make Async Web Service you need to implement IAsyncResult BeginOperation object EndOperation methods for your service

So my idea is that your Web Service is synchronous, but ajax requests are async.

Upvotes: 1

Abel Rodr&#237;guez
Abel Rodr&#237;guez

Reputation: 111

Yes, jQuery ajax calls are asynchronous by default.

If you need synchronous requests, set async option to false:

<script type="text/javascript">
$(document).ready(function () {      
    $('#btnTest').click(function () {
        $('#btnTest').hide();
        $.ajax({
            type: 'POST',
            async: false
            url: "request.asmx/newSync",
            data: '',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            complete: function () {
                $('#btnTest').show();
            },
            failure: function (msg) {
                $('#result').text(msg);
                //alert(msg);
            }
        });
    });
});

Upvotes: 0

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93551

It is async and is all handled for you :)

Your ajax call returns immediately after queuing the request.

It's specified complete: callback gets called when the response returns from the server, or error: (not failure) if an error occurs on the server.

in the meantime your browser/client code has been busy processing multiple execution loops.

Upvotes: 1

Related Questions