Ren
Ren

Reputation: 1503

ASP.NET MVC - Issue when receiving the Json response from IIS

Please see the screenshot below. I'm returning Json result from a controller to populate a cascading drop-down. The application works perfectly fine when run from the visual studio, but the drop-down doesn't get filtered at all (throws a 404) when run from the iis server. I'm really confused. Any help would be appreciated. Thanks.

In the pic, the one on top is when run from VS, and the one in the bottom is when run from the iis through intranet.

I've attached the code below as requested, but like I've mentioned above, it works perfectly fine when run from Visual Studio.

Controller

[HttpPost]
       public JsonResult FilterByDivision(string divisionId)
        {
            try
            {
                using (ctx)
                {
                    var SubDivResults = (from q in ctx.Test
                                           where q.divId== divisionId
                                           select new Models.SubDivisionDTO
                                           {
                                               SubDivID = q.subdivId,
                        SubDiv = q.SubDiv

                                           }).Distinct().ToList();
                    ajaxLookup.SubDiv = SubDivResults;           


                }
                JsonResult result = new JsonResult();
                    result.Data = ajaxLookup;
                      return result;
                }

            catch (Exception)
            {
                throw;
            }
        }

Jquery

//Filter By Division

$(function () {
$("select#DivisionId").change(function (evt) {
if ($("select#DivisionId").val() != "-1") {
$.ajax({
url: "/AjaxDropDown/FilterByDivision",
type: 'Post',
data: { divisionId: $("select#DivisionId").val() },
success: function (data) {
var subDivItems = "<option value='" + "-1" + "'>" + "Please Select" + "</option>";
$.each(data.subDivisions, function (i, val) {
subDivItems += "<option value='" + val.subDivId + "'>" + val.subDivName + "</option>";
});
$("select#subDivId").empty().html(subDivItems);
}
});
}
});
});

enter image description here

Upvotes: 2

Views: 1478

Answers (2)

Anderson Fortaleza
Anderson Fortaleza

Reputation: 2459

If you are making the call from jQuery, for example, check the path because you're getting a 404. You are probably running your application inside a virtual directory and you are calling a absolute path from your javascript.

That is probably your problem for a 404 result.

From your snippet the problem lies probably here:

$.ajax({
    url: "/AjaxDropDown/FilterByDivision",
    ...
});

You should have something probably on this form:

$(function () {
    $("select#DivisionId").change(function (evt) {
        var resourceAddress = '@Url.Content("~/AjaxDropDown/FilterByDivision")';

        if ($("select#DivisionId").val() != "-1") {
            $.ajax({
                url: resourceAddress,
                type: 'Post',
                data: {
                    divisionId: $("select#DivisionId").val()
                },
                success: function (data) {
                    var subDivItems = "<option value='" + "-1" + "'>" + "Please Select" + "</option>";
                    $.each(data.subDivisions, function (i, val) {
                        subDivItems += "<option value='" + val.subDivId + "'>" + val.subDivName + "</option>";
                    });
                    $("select#subDivId").empty().html(subDivItems);
                }
            });
        }
    });
});

This would give you a relative path every time.

Upvotes: 5

JamieSee
JamieSee

Reputation: 13010

Look in your IIS log files and check the HTTP Substatus Code. It will provide some additional information about what is causing the 404 error. See The HTTP status code in IIS 7.0, IIS 7.5, and IIS 8.0. If your substatus code is 0 or 1, you may not have the correct URL or your file may actually be missing from the server. If it's anything else you have other problems and posting the substatus code will help.

If you do have a substatus code of 0 or 1, Anderson Fortaleza is probably correct in his answer.

Upvotes: 0

Related Questions