2

In my web app I am making a AJAX call to an asynchronous webmethod that does not return.

Default.aspx has async="True"

The asp script:

$.ajax({
    type: "POST",
    url: "Default.aspx/Process",
    data: params,
    contentType: "application/json; charset=utf-8",
    success: function (data) {
         $("#AjaxDiv").text(data.d);
    },
    error: function(textStatus, errorThrown) {
          $("#AjaxDiv").text(textStatus.responseText);
          alert.text(textStatus.responseText);
    }  
});

On the server, the code is:

[WebMethod]
public static async Task<string> Process(int id)
{
    var data = await DataInterface.Call(id);
    return data.ToString();
}

However, DataInterface.Call() never returns, although via the debugger, I can see that Call() does complete. What settings or attributes might I be missing? whether it be in the app/web configs or page settings.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
C-Trouble
  • 21
  • 2
  • Ajax call itself is asynchronous, do you also need another async method on the server side? I would just use a regular method instead. – estinamir Apr 12 '20 at 20:35
  • The data interface getter is async and is not for me to change. I did try switching to a regular method and then using ```var data = DataInterface.call(id).GetAwaiter().GetResult();``` with the same conclusion, a runaway orphan. – C-Trouble Apr 13 '20 at 00:07
  • May be Ajax callback not possible, try postbackor use update panel callback. – estinamir Apr 13 '20 at 00:18
  • I tried UpdatePanel and the same issue occurred. I suspect this is because UpdatePanel is interpreted AJAX. Similarly I then moved the ASP version of the button outside of the UpdatePanel and the data call ran fine. But the forced page refresh triggered by an ASP button erases page state, which is why I initially tried AJAX/HTML over the ASP controls. – C-Trouble Apr 13 '20 at 06:13
  • Please double check your code against this example https://stackoverflow.com/a/21499435/10634638 – estinamir Apr 13 '20 at 14:26
  • was there ever a solution to this? I have exactly the same issue – Dave Becker Mar 11 '21 at 12:45
  • I found it: > [asp-net-call-asynchronous-webmethod-with-jquery](https://stackoverflow.com/questions/24370436/asp-net-call-asynchronous-webmethod-with-jquery) – M Safari Apr 21 '21 at 05:42

0 Answers0