简体   繁体   中英

ajax call calling an mvc async method never returns

i have an ajax call in my view chtml calling an async method in my controller. The problem is that the result is never returned even though the controller method does do a return. So in the browser it keeps waiting for the response and the ajax success or complete method are never called. It works if i do it synchronously. I am using mvc 4, .net 4.5.

In my view i have the following ajax:

$.ajax({
   type: "POST",
   url: '@Url.Action("PopulateCertificatesViewModel")',
   data: data,
   success: function(certificatesViewModel) {
     alert('success');
     //do stuff with the response data...

   },
  complete: function() {
    alert('complete');
});

In my controller i have the following method:

[HttpPost]
public async Task<JsonResult> PopulateCertificatesViewModel(int orderId)
{
   var service = getservice();

   await Task.Run(() =>
            {                    
                response = service.getData();
            });

   return Json(response);
}

I had a similar problem. My request would return from the controller and would just be in a pending status in the browser with the content from the request never showing up. I had made sure I was using MVC 4 and .NET 4.5 and had the <httpRuntime targetFramework="4.5" /> set in my web.config.

In the end what fixed it for me was using IISExpress instead of the Cassini web server that comes with Visual Studio.

Hope this helps.

try $.ajax with contentType :'application/json' . I should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM