My head is about to explode trying to figure out what the problem is here. I've tried everything I can think of, as well as a few unthinkable things...
I had a .NET MVC application in C#, originally using .NET Core 2.0, built using VS 2017. I needed to add https support to it, which meant I had to migrate it to .NET Core 3.1 and VS 2019. I created a new application in VS 2019, copied the old code in, and updated any calls/classes required by the framework changes.
The application makes several jQuery.Ajax calls to controller methods, all of which were working in the .NET Core 2.0 application. They worked both locally and on the hosting server (SmarterASP.net.)
They are all also working in the new application except one, and that one works fine locally -- but not when published to the host. I wanted to see if anyone has seen anything similar and can suggest anything. I will also contact the host, but don't expect much there.
This is the controller method, which simply gets a list of product categories and returns it as a JSON object:
public class ProductsController : Controller
{
//...
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult GetCategoryTreeJson()
{
List<BemsCategory> categories = GetCategoryTree("0");
return Json(categories);
}
This is the ajax call:
function retrieveCategoryTreeJson(callOnSuccess, callOnComplete) {
$.ajax({
url: "/Products/GetCategoryTreeJson",
data: AddAntiForgeryToken({}),
type: "POST",
dataType: "json",
success: function (response) {
if (callOnSuccess && response != undefined) {
callOnSuccess(response);
}
},
error: function (xhr, status, thrownError) {
handleAjaxError(xhr, $("#main-error-result"));
},
complete: function (xhr, status, thrownError) {
if (callOnComplete)
callOnComplete();
}
});
}
The result is "Failed to load resource: the server responded with a status of 404 (Not Found)" in the browser Developer Tools console.
The call drops to the ajax error block, but there's no useful information in it -- just "error" in the status param. I have many other methods in many controllers, with ajax calls to them, and they all follow the same form as this one and are working.
I added a Test() method to the controller (with corresponding ajax call,) first like this:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Test()
{
return Json(new { Info = "hello" });
}
This simple test worked. I thought that maybe the JSON packaging in the original method -- return Json(categories) -- was causing a problem, so I tried serializing the data and embedding the string in an outer JSON wrapper:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Test()
{
List<BemsCategory> categories = GetCategoryTree("0");
string temp = Newtonsoft.Json.JsonConvert.SerializeObject(categories);
JsonResult res = Json(new { Info = temp });
return res;
}
This resulted in the same 404 error. The string returned from SerializeObject() looks as it should. I then thought that maybe the length of the data (about 28K) was the problem -- perhaps the server's IIS sets a limit, or something. So I tested that by using a string literal of the entire actual data that SerializeObject() returns:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Test()
{
string temp = "[{"Id":"01","ParentId":"0","Name":"Sheet Music & Scores","NumChildren":12,"NumProducts":0,"Children": ... " // 28K total length string, concatenated for display here.
return Json(new { Info = temp });
}
This worked!! The ajax call hits the success block, and response.Info contains the JSON string. The strings named "temp" in the last two examples are identical. I can't for the life of me figure out what's going on here! Again, this error only occurs on the hosting server. Locally everything works.
Help me Obiwan Kenobi -- you're my only hope.
question from:
https://stackoverflow.com/questions/65545459/jquery-ajax-call-returns-404-not-found-after-migrating-from-net-core-2-0-to