我有一个带有几个级联下拉列表的Web应用程序。所以我使用ajax来更新下一个下拉列表的值。我把javascript脚本放在一个单独的文件中。
// Code that triggers when there is a change in Activity drop down.
$('#ActivityId').change(function () {
var activityId = $(this).val();
// Empty the Workstation.
$('#WorkstationId').empty();
$('.showhide-workstation').show();
var url = "~/WorkOrderSubmissions/GetWorkstationsByActivityJson";
// AJAX call that re-populate Workstation drop down depending on the Activity selected.
$.ajax({
type: 'POST',
url: url,
dataType: 'json',
data: { activityId: activityId },
success: function (codes) {
$('#WorkstationId').append('<option value=""></option>');
$.each(codes, function (i) {
$('#WorkstationId').append('<option value = "' + codes[i].Value + '">' + codes[i].Text + '</option>');
});
},
error: function (ex) {
$('#WorkstationId').append('<option value=""></option>');
}
}); // END $.ajax() on GetRejectCodesByActivityJson
}); // END $('#ActivityId').change()
当我从Visual Studio内部运行代码时,代码可以正常工作。当我将其部署到本地Web服务器时,它不起作用。我将其部署到
http://localhost/mea
当我在Chrome中打开开发者工具时,我看到了错误。
POST http://localhost/~/WorkOrderSubmissions/GetLinesByWorkorderJson 404 (Not Found)
我试图将网址更改为
url = ~/WorkOrderSubmissions/GetLinesByWorkorderJson
url = /WorkOrderSubmissions/GetLinesByWorkorderJson
url = WorkOrderSubmissions/GetLinesByWorkorderJson
它们都不起作用。我以为〜应该转到Web应用程序的根目录,即http://localhost/mea。
答案 0 :(得分:3)
~
语法仅由ASP.Net识别;它将由任何JS代码按字面意思理解。您需要通过C#代码向JS提供URL:
var url = '@Url.Content("~/WorkOrderSubmissions/GetWorkstationsByActivityJson")';
或者更好:
var url = '@Url.Action("GetWorkstationsByActivityJson", "WorkOrderSubmissions")';