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.
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: