0

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

2
  • 1
    Your javascipr is async, it will not block on sending the request and it will fire complete or failure callbacks when the response comes back. Commented Dec 5, 2014 at 11:27
  • 1
    Note it is error: and not failure: for any ajax errors. Commented Dec 5, 2014 at 11:28

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @TrueBlueAussie. Correct me if i'm wrong. This is my scenario, I'm suppose to calculate yearly billings. Alright, now once i click the run button, a request has already been made meaning it will run the billing in the background right(i havent add the codes here, let's imagine)? Is this correct?
@Gopinath Koothaperumal: Yes. If you use a tool like Fiddler2 or the Chrome F12 network panel, you can see the timing of the request/response is "relatively" slow (takes a significant fraction of a second) but the client browser code just keeps on running.
1

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.

Comments

0

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);
            }
        });
    });
});

2 Comments

Never, repeat never, suggest async: false as a solution to anything. Always encourage working with the async default pattern :)
Yes I know and you're right, but it's an option there, he did not know how jQuery ajax calls work and this is an option for them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.