Jquery ajax发布后不工作

Jquery ajax发布后不工作,jquery,asp.net-mvc-3,Jquery,Asp.net Mvc 3,下面是我的代码,它在VisualStudio中工作,但在我将其发布到测试环境时不工作 myview.cshtml JavaScript MyController.cs 它进入错误处理程序并显示警报消息Not found我在测试环境中调试了JavaScript它有staus 404我建议您使用@Url.Action 它生成操作方法的完全限定URL 如果您的代码在razor视图中,而不是js文件中,则可以使用此方法。如果您正在js文件中调用函数。您可以通过razor代码将url传递给该函数。问题已解

下面是我的代码,它在VisualStudio中工作,但在我将其发布到测试环境时不工作

myview.cshtml

JavaScript

MyController.cs

它进入错误处理程序并显示警报消息Not found我在测试环境中调试了JavaScript它有staus 404

我建议您使用@Url.Action

它生成操作方法的完全限定URL


如果您的代码在razor视图中,而不是js文件中,则可以使用此方法。如果您正在js文件中调用函数。您可以通过razor代码将url传递给该函数。问题已解决:当我在JS中将url更改为“../Country/GetCitiesByCountryId”时,它起了作用


我认为它在我们的测试环境中不起作用,因为测试环境是一个虚拟目录,我们使用url重定向转到我们的测试

您在JS中创建了一个url变量,但在$.ajax调用中没有使用它。这是故意的吗?我并没有使用它,因为当我使用它时,我在我的Dev VS上得到了与我们的测试环境相同的错误。我将其更改为我使用的,然后在VS中工作。问题解决:当我在JS中将url更改为“../Country/GetCitiesByCountryId”时,它工作了谢谢你,尼廷,我已经测试过了,但它不工作。我认为它在我们的测试环境中不起作用,因为测试环境是一个虚拟目录,我们使用url重定向来转到我们的测试
@Html.DropDownListFor(m => m.CountryId,
        new SelectList(Model.Countries, "CountryId", "CountryName"),
        " -- please choose a country-- ",
        new { onchange = "CountryDDLChanged()",  @class = "form-control" })
  function CountryDDLChanged() {

var url = '@Url.Content("~/Country/GetCitiesByCountryId")';
var countryid = $("#CountryId").val();
var ddlTarget = $("#CityId");

$(ddlTarget).empty();

 $.ajax({
    type: "GET",
    url: "/Country/GetCitiesByCountryId",
    data: { countryId: countryid },
    success: function (result) {
        ddlTarget.append("<option  value=''> -- please choose a city -- </option>");

        $.each(result, function (index, city) {
            ddlTarget.append("<option value='" + city.CityId + "'>" + city.CityName+ "</option>");
        });

        $("#cityDiv").show('slow');
    },
    error: function (req, status, error) {
        alert(error);
    }
 });
}
 [AllowAnonymous]
 public JsonResult GetCitiesByCountryId(int countryId)
    {
        JsonResult result = new JsonResult();
        using (var db = new PetaPoco.Database("myconnection"))
        {
            List<City> cities = db.Fetch<City>("Select * from City where CountryId = @0", countryId);
            result.Data = cities;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
        }
        return result;
    }
@Url.Action("GetCitiesByCountryId","Country")