0

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.

1
  • Incidentally, the option value you have set; async: true is the default value. Commented Apr 11, 2012 at 13:50

3 Answers 3

3

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Dan... Thank you for that clear-cut information. But I've one more doubt in that. Imagine my webservice contains some long running codes and we had made that webservice call using ajax. My doubt is that, will that webservice code execution suspends if the user closes that browser window or will it get continue till all the codes execute in webservice even after the browser closed?
@user954093 I can't say with authority since I've never run into the issue before, but I assume your server side process will still finish executing independent of the browser status in which it was called. Unless, of course, you are tracking session status on the server side and explicitly looking to abort with an ended session.
Great! If you are satisfied with my answer, you should accept it as the answer (clicking on the hollow checkmark below the voting buttons). I get points, you get a badge, we all win. :)
2

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!

Comments

-1

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.

1 Comment

Hey @AndrewBarber i was just cross checking! sometimes it happens we work all the way round and forget a simple line.

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.