以下是我在Visual Studio中工作的代码,但在将其发布到我们的测试环境时无法工作。ajax在发布后不工作
myview.cshtml
@Html.DropDownListFor(m => m.CountryId,
new SelectList(Model.Countries, "CountryId", "CountryName"),
" -- please choose a country-- ",
new { onchange = "CountryDDLChanged()", @class = "form-control" })
的JavaScript
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);
}
});
}
MyController.cs
[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;
}
它去到错误处理程序并显示警告信息没有找到我调试JavaScript的测试环境有居留制404
你创造你的JS'url'变量,但没有不要在'$ .ajax'调用中使用它。那是故意的吗? – jwatts1980
我没有使用它,因为当我使用它时,我的Dev(VS)上的测试环境出现同样的错误。我将它改为我使用的那个,然后在VS中工作。 – user217648
问题解决:它的工作原理当我在我的JS – user217648