Search code examples
c#model-view-controllerasp.net-ajaxasp.net-core-6.0

Upgrading Web App from 3.1 to 6.0 on Net Core Causes Ajax call issue


So i recetly upgraded our application from Net core 3.1 to 6.0. So i updated the framework from 3.1 to 6.0 and updated the package references after that Ajax calls stopped working. It does not reach controller and returns HTML on success. Below is my action in controller. [HttpPost] public JsonResult SamplePostCall(string PostData) { return Json(PostData); }

$.ajax({ type: 'POST', url: "/Home/SamplePostCall", data: { PostData: "Sample Post Data" }, success: function(response) { console.log(response); }, error: function(err){ console.log(err); } });

success function return current page html in it. enter image description here


Solution

  • I was able to resolve it by adding the route on Http call attribute on controller. So basically i had to update my old code with route like below and it works now. Hope this helps someone else too. but Do anyone know why this happened? did they changes anything in 6.0 that cause this? on older version it works fine.

    [HttpPost("/Home/SamplePostCall")]

    public JsonResult SamplePostCall(string PostData)
    {
       return Json(PostData);
    }