1

I'm encountering an issue where my AJAX call isn't reaching the controller method in my ASP.NET Core application. Despite setting up the AJAX call and controller method, I'm receiving a 404 error. Here's my controller method and JavaScript function:

[HttpPost]
public async Task<ActionResult> GetCityConfigurations(DSParamsDto dSParams)
{
    // Your controller logic here
    var citys = await cityService.Getcitys(dataTableParams);
    return Json(new JqueyDataTableResponseModel<cityDto>
    {
        draw = dataTableParams.Draw,
        recordsTotal = citys.Total,
        recordsFiltered = citys.Total,
        data = citys.Items
    });
}
function GetcitySapConfigurations() {
    var token = $('input[name="__RequestVerificationToken"]').val();

    citySapConfig = $("#tblcitySapConfiguration").DataTable({
        "processing": true,
        "serverSide": true,

        "ajax": {
            "url": 'GetCityConfigurations/',
            "type": "POST",
            "datatype": "json",
            "headers": { "XSRF-TOKEN": token },
            "error": handleAjaxErrorLoc
        },
        "columns": [
            {
                "data": "cityId"
            },
            { "data": "cityName" }
        ]
    });
}

I've confirmed that the URL in the AJAX call ('GetCityConfigurations/') is correct, and I've included the necessary anti-forgery token. However, the request doesn't seem to reach the controller action. Any insights into what might be causing this issue would be greatly appreciated. Thank you!

4
  • Have you tried sending the token back as a different header? I've always used "RequestVerificationToken". Commented Jun 14, 2024 at 14:17
  • You have two code bases running on two systems: JavaScript in your browser, and C# on your server. The problem is likely to be in one of them. Thus, using standard troubleshooting techniques, try to isolate the problem to one of these systems. Use your browser's F12 tools to see what the HTTP request is. Does it look okay? What happens if you issue an identical HTTP request with another tool (e.g. curl)? Commented Jun 14, 2024 at 14:29
  • All that said, since the response is 404 Not Found my guess is that the client-side code requests a URL that the ASP.NET code base doesn't recognize. Feel free to update the question once you have more information to share. Commented Jun 14, 2024 at 14:31
  • I dont know where is issue coming from i tried with debugger as well. Commented Jun 14, 2024 at 15:25

1 Answer 1

0

Despite setting up the AJAX call and controller method, I'm receiving a 404 error.

404 error means your request url is not correct.

Press F12 in the browser and check the Network panel which request you send, or you can check the Console panel. enter image description here

You can specify the URL relative to the root of your application by starting the URL with a /. This will ensure that the request is made to the correct endpoint regardless of the current page's URL.

"url": '/GetCityConfigurations',

Or you can use an absolute URL, though this is less flexible if your site's base URL changes:

"url": 'https://localhost:portNumber/GetCityConfigurations',
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.