Sandeep Thomas
Sandeep Thomas

Reputation: 4727

Async AJAX calls using jQuery ASP.NET not working

I've created a webservice to the which the calls is from a jquery ajax function. But even when async set to true it is not working asynchronously..

My ASP.NET webservice code

<System.Web.Services.WebMethod()> _
Public Shared Function sampleService(ByVal ttid As String) As String
Threading.Thread.Sleep(5 * 1000)
Return "Hello World"
End Function

JQuery Call script

<script language="javascript">
$(function() {
    var tempParam = {
        ttid: 100
    };

    var param = $.toJSON(tempParam);
    $.ajax({
        type: "POST",
        url: "testservice.aspx/sampleService",
        data: param,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        error: function() {
            alert("Error");
        },
        success: function(msg) {
            alert("Success")
            alert(msg.d)
        }
    });
});​    </script>

Here I set it as async = true. Even then I'm getting the success message after 5 seconds. That means not asynchronous. What I believe is if async = true it will not wait for the message from the webserivice. That is actually my requirement.

Upvotes: 0

Views: 6637

Answers (3)

Dan A.
Dan A.

Reputation: 2924

The success function is a callback; it's designed to be called AFTER a response has been received. How could you determine success or error if it was called before the server thread execution had completed? The call to Sleep suspends the current server thread, so of course your response is going to take five seconds to come back.

The asynchronous part would apply to Javascript code that directly follows your ajax post. For example:

<script language="javascript">
$(function() {
    var tempParam = {
        ttid: 100
    };

    var param = $.toJSON(tempParam);
    $.ajax({
        type: "POST",
        url: "testservice.aspx/sampleService",
        data: param,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        error: function() {
            alert("Error");
        },
        success: function(msg) {
            alert("Success")
            alert(msg.d)
        }
    });
    alert('This alert is asynchronous!  We do not know yet if the ajax call will be successful because the server thread is still sleeping.');
});​    </script>

Upvotes: 3

gdoron
gdoron

Reputation: 150253

Here I set it as async = true. Even then I'm getting the success message after 5 seconds. That means not asynchronous. What I believe is if async = true it will not wait for the message from the webserivice.

No, async means the working thread is locked and won't execute no other code(and may freeze the window...) until it get the response from the server for its request.
It doesn't mean you will get an answer i a moment at all!

Upvotes: 2

Murtaza
Murtaza

Reputation: 3065

Have you checked that the webservice is set to execute in script.

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 

[System.Web.Script.Services.ScriptService]

Please check this and send an update if it working or not.

Upvotes: -1

Related Questions