Javascript only working locally, called function returns 404 error from server after publish

I've made some cascading dropdown lists and after at least a week with them I got them to work. So I published the project and tested it but then it didn't work. The second list never populates.

Firebug's console tells me "/Companies/GetBolag 404 Not Found" but not when I test it locally. Then it works just fine. I've tried adding [HttpPost] and [AcceptVerbs(HttpVerbs.Get)] before my JsonResult but to no avail. What am I missing?

Javascript in view:

<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">

    $(document).ready(function () {
        $("#bolag").prop("disabled", true);
        $("#Kund").change(function () {
            if ($("#Kund").val() != "") {
                var options = {};
                options.url = "/Companies/GetBolag";
                alert("01");
                options.type = "POST";
                options.data = JSON.stringify({ country: $("#Kund").val() });
                options.dataType = "json";
                options.contentType = "application/json";
                alert($("#Kund").val());
                options.success = function (bolag) {
                    $("#bolag").empty();
                    alert("03");
                    for (var i = 0; i < bolag.length; i++) {
                        $("#bolag").append("<option>" + bolag[i] + "</option>");
                    }
                    $("#bolag").prop("disabled", false);
                }; 
                options.error = function () { alert("Fel vid bolagshämtning!"); };
                $.ajax(options);
            }
            else {
                $("#bolag").empty();
                $("#bolag").prop("disabled", true);
            }
        });
    });

</script>

JsonResult function in controller:

public JsonResult GetBolag(string country)
        {
            try
            {
                string connectionString = @"Provider=SQLOLEDB.1;Data Source=GRAHAM;Database=CompaniesDB;User Id=****;Password=****";
                string query = @"SELECT DISTINCT Underkund FROM [CompaniesDB].[dbo].[Småbolag] WHERE Huvudkund = '" + country + "'";
                object[] temp = new object[1];
                List<string> bolag = new List<string>();

                using (OleDbConnection connection = new OleDbConnection(connectionString))
                {
                    connection.Open();

                    OleDbCommand command = new OleDbCommand(query, connection);
                    OleDbDataReader dr = command.ExecuteReader();

                    bolag.Add("");

                    while (dr.Read())
                    {
                        dr.GetValues(temp);
                        bolag.Add(Convert.ToString(temp[0]));
                    }

                    connection.Close();
                    connection.Dispose();
                }

                return Json(bolag, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                var path = @"C:\log.txt";

                if (!System.IO.File.Exists(path))
                {
                    using (var sw = System.IO.File.CreateText(path))
                    {
                        sw.WriteLine(ex.Message);
                    }
                }
                return Json(null);
            }
        }

The random alert()s in the javascript is just to check where the error lies. When published alert("01"), alert($("#Kund").val()) and alert("Fel vid bolagshämtning!") fires.

I'm sure there is a lot of bad practice in this code, but I got my position as a developer at my work without any prior experience, it's been trial-and-error, learn-by-doing as I've been handed projects and such. I mainly just need to get things to work. I know that that isn't really the way to go about this, but please help me out!

Answers


After a couple of days of googleing, reading and testing i found my solution here.

I changed the line:

options.url = "/Companies/GetBolag";

to:

options.url = ("@Url.Content("~/Companies/GetBolag")");

And now my cascading drop downs work both locally and on the server. I guess that this is what was suggested in the comments but I just didn't understand it.


Need Your Help

simple jquery callback works on .net3.5 but nothing fires on .net 2.0?

c# asp.net jquery

i have the following jquery postback method on the client

Shell Script: Speed of transfer of a hop using command Ping

bash shell unix ping cut

Create a shell script that relates the speed of transfer of a HOP between the machine and the IP chosen. Use the PING command and express the result in kB/sec.