Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

584
Views
AJAX call returns 404 on server, works locally

In my application I have a drop-down menu that lists various customers, and a second drop-down that will list the cities with which a given customer is associated. Selecting a customer should populate the second drop-down with the relevant list of locations. This happens by triggering an Ajax call to a method in the controller, which passes the name of the selected customer in variable "loc". The list of locations is obtained via an EF Core query.

This all works as expected on my local machine, but when running on our server it returns a 404 - Not Found error.

The Ajax call looks like this:

$.ajax({
     url: "/[Controller]/GetLocations",
     type: "POST",
     dataType: "text",
     data: { 'loc': loc },
     success: function (data, textStatus, jqXHR) {

        var listData = JSON.parse(data);
        resetLocationList();
        for (var listSize = 0; listSize < listData.length; listSize++) {
             $('#ffBankLocation').append('<option value="' + listData[listSize] + '">' + 
             listData[listSize] + '</option>');
        }

     },
     error: function (jqXHR, textStatus, errorThrown) {
        console.log('Error caught: ');
        console.log(errorThrown);
     }
});

And the method in the controller looks like this:

[HttpPost]
public async Task<JsonResult> GetLocations(string loc)
{
    var locations = await _repo.GetLocationsByName(loc);

    return Json(locations);
}

Finally, the URL as reported in the 404 error looks like this:

https://[URL]/[Controller]/GetLocations?loc=Some%20Location

Given that the URL value isn't being found, I doubt the settings after that have anything to do with the problem. The only thing I could think to change there is that the controller was contained in a sub-folder within the Controllers folder (Controllers/myFolder/Controller), but moving the controller to the main Controllers folder, no sub-folder in between, doesn't resolve the issue.

Any advice appreciated.

EDIT: To be clear about moving the controller, I did not move it from the main Controllers folder, I only moved it out of the sub-directory within the Controllers folder in which it was previously found.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

try to remove [HttpPost], and add attribute route

[Route("~/mycontroller/GetLocations")]
public async Task<JsonResult> GetLocations(string loc)

if it is still not working try to change ajax to get

$.ajax({
     url: "/[Controller]/GetLocations?loc="+loc,
     type: "GET",
     dataType: "json",
     success: function (data, textStatus, jqXHR) {

and never ever move controllers from default Controllers folder

over 4 years ago · Santiago Trujillo Report

0

Problem solved. I needed to convert my drop-down list into an @Html.DropDownListFor object, and wrap it in a form object like so:

<form id="custNameForm" asp-controller="myController" asp-action="GetLocations" method="post">
     @Html.AntiForgeryToken()

     @Html.DropDownListFor(x => x.Name, new SelectList((List<string>)ViewBag.custList), "Select Customer",
          new { @class="form-control text-center" })
</form>

Next, in my javascript file, I add the following "change" listener:

.on('change', '#CustName', function (e) {
     e.preventDefault();
     GetLocations(e);
})

...which points to this jquery function:

function GetLocations(e) {
    var loc = $('#CustName option:selected').val();
    var noLoc = loc === '' || loc === 'Select Customer';

    if (!noLoc) {
        var form = $('#custNameForm');
        var url = form.attr('action');
        var token = $('input[name="__RequestVerificationToken"]').val();

        $.ajax({
            url: url,
            type: "POST",
            dataType: "json",
            data: {
                __RequestVerificationToken: token,
                'loc': loc
            },
            success: function (data, textStatus, jqXHR) {

                var listData = data;
                resetLocationList();
                for (var listSize = 0; listSize < listData.length; listSize++) {
                    $('#custLocation').append('<option value="' + listData[listSize] + '">' + listData[listSize] + '</option>');
                }

            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log('Error caught: ');
                console.log(errorThrown);
            }
        });
    }
    else {
        parseXML('');
    }

    $('#custLocation').prop('disabled', noLoc);
}

... where "#custLocation is the ID of the dropdown to be populated with the list of customer locations.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!