![](/img/trans.png)
[英]Failed to load resource: the server responded with a status of 500 (Internal Server Error
[英]Jquery ajax call wont access the controller: Failed to load resource: the server responded with a status of 500 (Internal Server Error)
我一直无法加载资源:服务器响应状态为500(内部服务器错误),指示对我的控制器方法的调用不起作用,我不知道为什么:
var user = "founder";
var clanName = "superClan";
var SuspensionDate = Date.now;
$.ajax({
url: '@Url.Action("SuspendUserFromClan", "ChatMethods")',
type: "POST",
contentType: "application/json; charset=utf-8",
data: { 'ClanName': clanName, 'UserToSuspend': userToAdd, 'DateSuspendedTill': SuspensionDate },
dataType: "json",
traditional: true,
success: function (data, status, xhr) {
alert(data);
},
error: function () {
alert("An error has occured!!!");
}
});
我的控制器:
public JsonResult SuspendUserFromClan(string ClanName, string UserToSuspend, DateTime DateSuspendedTill)
{
...
}
在执行操作时将[HttpPost]添加到控制器中:从ajax输入:“ POST”
[HttpPost]
public JsonResult SuspendUserFromClan(string ClanName, string UserToSuspend, DateTime DateSuspendedTill)
{
...
}
var SuspensionDate = new Date();
$('#SuspendUser').on("click", function () {
$.ajax({
url: '@Url.Action("SuspendUserFromClan", "ChatMethods")',
type: "POST",
// contentType: "application/json; charset=utf-8",
data: { 'ClanName': clanName, 'UserToSuspend': userToAdd, 'DateSuspendedTill': SuspensionDate.toUTCString() },
dataType: "json",
traditional: true,
success: function (data, status, xhr) {
alert(data);
},
error: function () {
alert("An error has occured!!!");
}
});
});
解决了这个问题。
该错误位于MVC Razor引擎或其他地方。 我可以解释一下并重新创建错误。 我收到错误:
无法加载资源:服务器的状态为500(内部服务器-我的网址-
奇怪的是,在我向PartialView添加一些Razor代码之前,它一直在起作用。
我的局部视图以前看起来像(为简洁起见,省略了某些部分):
@model DataDisplay.Models.AllItems
// ....
@foreach (var item in Model)
{
<tr ><td>@item.ID</td><td>@item.UserName</td<td>@item.FullPath</td></tr>
}
我添加到Razor的简单代码似乎给了我一个错误,它是一个if语句,该语句使我的代码如下所示:
@model DataDisplay.Models.AllItems
// ....
@if (Model.Count > 0)
{
@foreach (var item in Model)
{
<tr ><td>@item.ID</td><td>@item.UserName</td<td>@item.FullPath</td></tr>
}
}
也许您已经看到了不正确的Razor代码,但是请继续。
添加代码后,我立即得到500错误,并且在Chrome开发工具中也得到一个指示,指示jQuery中出现jQuery错误,该错误在jQuery脚本中的一行显示如下:
xhr.send((s.hasContent && s.data)|| null);
是的,我正在调用jquery.load()在当前页面中加载partialview。 我四处搜索,然后找到以下SO条目,将其引导至我的答案: Post and Get With Same签名
但是,使我解决问题的答案是莫尔坦的答案 。
我将代码添加到控制器中:
[AcceptVerbs("Get", "Post")]
然后,当我运行时,我在构建时收到警告:
警告11在“ @”字符后出现意外的“ foreach”关键字。 进入代码后,您无需在“ foreach”之类的结构前面加上“ @”前缀。 filename.cshtml 6 10类名
请注意,这会导致500错误,但仅在编译时发出警告。
我从foreach中删除了多余的@字符,一切正常。
这一切似乎都像是一场完美的错误风暴,它会导致错误的红鲱鱼,因此我希望这对某人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.