我在Visual Studio 2013中创建了一个MVC3项目。视图引擎是razor.First我在@section Scripts {}中编写jquery.ajax视图(cshtml)它工作正常。但我在.js文件中分离脚本并调试我得到错误: (调试IIS Express)
IIS 8.0详细错误 - 404.0 - 未找到 ... 更多信息:此错误表示服务器上不存在该文件或目录。创建文件或目录并再次尝试请求 ..
我的jquery函数(archive.js):
jQuery.AjaxGetProjects = function (dropdownId, detailDropDownId, authorized) {
var projectId = $(dropdownId).val();
if (projectId != null && projectId != '') {
var url =
$.ajax({
type: "POST",
url: '@Url.Action("GetProjects", "Archive")',
data: {
'projectId': projectId,
'authorized': authorized
},
success: function (departman) {
var length = 0;
$(detailDropDownId).empty();
$.each(departman, function (index, proje) {
length = length + 1;
$(detailDropDownId).append($('<option/>', {
value: proje.Value,
text: proje.Text,
selected: proje.Selected
}));
});
if (length == 2) {
$(detailDropDownId).trigger('change');
}
},
error: function (xhr, ajaxOptions, thrownError) {
// bu kısımda eğer ajax işlemi başarısız ise
// hata mesajı verebiliriz.
alert(xhr.responseText);
}
});
}
}
控制器:
public class ArchiveController : BaseController
{
...
public ActionResult GetProjects(int projectId, bool authorized)
{
IArchive arch = WcfServiceHandler.GetDmsService<IArchive>();
List<Poco> list = arch.GetProjects(UserManager.GetUserInfo(), projectId);
var t = MvcHelper.GetDropDownList<Poco>(list, "NAME", "ID", "");
return Json(t, JsonRequestBehavior.AllowGet);
}
我尝试了这个但没有用(我收到了一个新错误:无法找到资源):
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
请帮忙..
答案 0 :(得分:5)
您不能在JavaScript文件中使用Razor(除非您使用类似RazorJS扩展名的内容)。如果你在浏览器开发工具中查看你的JS,你会看到输出的网址是:
url: '@Url.Action("GetProjects", "Archive")',
应该是:
url: www.yoursite.com/Archive/GetProjects
要修复它,您可以从视图中调用该JS进行方法调用并传入URL,例如:
查看:
<script>
callMethod('@Url.Action("GetProjects", "Archive")');
</script>
那你的JS就是:
function callMethod(url) {
....
或者如果失败了,你将硬编码编码为:
url: '/Archive/GetProjects'
但是如果要部署到子目录中的站点,请小心,该链接无效。
答案 1 :(得分:1)
javascript无法使用razor语法,因此@ Url.Action(“GetProjects”,“Archive”)将无效,请尝试将帖子URL作为参数传递或硬编码,例如。
jQuery.AjaxGetProjects = function (dropdownId, detailDropDownId, authorized) {
var projectId = $(dropdownId).val();
if (projectId != null && projectId != '') {
var url =
$.ajax({
type: "POST",
url: '/Archive/GetProjects',
data: {
'projectId': projectId,
'authorized': authorized
},
success: function (departman) {
var length = 0;
$(detailDropDownId).empty();
$.each(departman, function (index, proje) {
length = length + 1;
$(detailDropDownId).append($('<option/>', {
value: proje.Value,
text: proje.Text,
selected: proje.Selected
}));
});
if (length == 2) {
$(detailDropDownId).trigger('change');
}
},
error: function (xhr, ajaxOptions, thrownError) {
// bu kısımda eğer ajax işlemi başarısız ise
// hata mesajı verebiliriz.
alert(xhr.responseText);
}
});
}
OR
jQuery.AjaxGetProjects = function (dropdownId, detailDropDownId, authorized, postUrl) {
var projectId = $(dropdownId).val();
if (projectId != null && projectId != '') {
var url =
$.ajax({
type: "POST",
url: postUrl,
data: {
'projectId': projectId,
'authorized': authorized
},
success: function (departman) {
var length = 0;
$(detailDropDownId).empty();
$.each(departman, function (index, proje) {
length = length + 1;
$(detailDropDownId).append($('<option/>', {
value: proje.Value,
text: proje.Text,
selected: proje.Selected
}));
});
if (length == 2) {
$(detailDropDownId).trigger('change');
}
},
error: function (xhr, ajaxOptions, thrownError) {
// bu kısımda eğer ajax işlemi başarısız ise
// hata mesajı verebiliriz.
alert(xhr.responseText);
}
});
}