简体   繁体   English

jQuery ajax调用将无法访问控制器:无法加载资源:服务器以状态500(内部服务器错误)响应

[英]Jquery ajax call wont access the controller: Failed to load resource: the server responded with a status of 500 (Internal Server Error)

I keep getting Failed to load resource: the server responded with a status of 500 (Internal Server Error) indicating that the call to my controller method isnt working and I dont know why: 我一直无法加载资源:服务器响应状态为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!!!");
            }
        });

my controller: 我的控制器:

public JsonResult SuspendUserFromClan(string ClanName, string UserToSuspend, DateTime DateSuspendedTill)
{
 ...
}

add [HttpPost] to your controller as you are doing type:"POST" from ajax 在执行操作时将[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!!!");
                }
            });
        });

fixed this issue. 解决了这个问题。

The bug is in the MVC Razor engine or somewhere else. 该错误位于MVC Razor引擎或其他地方。 I can explain this and recreate the bug. 我可以解释一下并重新创建错误。 I was getting the error: 我收到错误:

Failed to load resource: the server responded with a status of 500 (Internal Server -- my URL -- 无法加载资源:服务器的状态为500(内部服务器-我的网址-

The odd thing was that it had just been working before I added some Razor code to my PartialView. 奇怪的是,在我向PartialView添加一些Razor代码之前,它一直在起作用。

My partial view previously looked like (some parts omitted for brevity): 我的局部视图以前看起来像(为简洁起见,省略了某些部分):

@model DataDisplay.Models.AllItems
// ....
@foreach (var item in Model)
   {
      <tr ><td>@item.ID</td><td>@item.UserName</td<td>@item.FullPath</td></tr>
   }

The simple code I added to Razor which seemed to give me an error was an if statement which made my code look like the following: 我添加到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>
     }
}

Maybe you already see the incorrect Razor code, but please continue on. 也许您已经看到了不正确的Razor代码,但是请继续。

Immediately after I added that code I got the 500 error and I also got an indication (in Chrome dev tools) that there was an error in jQuery which occurs in the jQuery script on a line which looks like : 添加代码后,我立即得到500错误,并且在Chrome开发工具中也得到一个指示,指示jQuery中出现jQuery错误,该错误在jQuery脚本中的一行显示如下:

xhr.send( ( s.hasContent && s.data ) || null ); xhr.send((s.hasContent && s.data)|| null);

Yes, I'm calling jquery.load() to load the partialview in my current page. 是的,我正在调用jquery.load()在当前页面中加载partialview。 I searched around and found the following SO entry which lead me to my answer: Post and Get With Same signature 我四处搜索,然后找到以下SO条目,将其引导至我的答案: Post and Get With Same签名

However, the answer that got me to the solution was the one by mohrtan . 但是,使我解决问题的答案是莫尔坦的答案

I added the code to my controller: 我将代码添加到控制器中:

[AcceptVerbs("Get", "Post")]

Then, when I ran, I got a warning when I went to build : 然后,当我运行时,我在构建时收到警告:

Warning 11 Unexpected "foreach" keyword after "@" character. 警告11在“ @”字符后出现意外的“ foreach”关键字。 Once inside code, you do not need to prefix constructs like "foreach" with "@". 进入代码后,您无需在“ foreach”之类的结构前面加上“ @”前缀。 filename.cshtml 6 10 classname filename.cshtml 6 10类名

Notice that this causes the 500 error, but is only a warning when compiling. 请注意,这会导致500错误,但仅在编译时发出警告。

I removed the extra @ character from the foreach and it all ran fine. 我从foreach中删除了多余的@字符,一切正常。

This all seems like a Perfect Storm of errors which causes a red herring of an error so I hope this helps someone. 这一切似乎都像是一场完美的错误风暴,它会导致错误的红鲱鱼,因此我希望这对某人有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加载资源失败:服务器响应状态为500(内部服务器错误 - Failed to load resource: the server responded with a status of 500 (Internal Server Error Ajax 调用“加载资源失败:服务器响应状态为 500” - Ajax call with a 'failed to load resource : the server responded with a status of 500' 加载资源失败:服务器响应状态为 500(内部服务器错误),同时从控制器返回 base64 格式的图像 - Failed to load resource: the server responded with a status of 500 (Internal Server Error) while returning image in base64 format from controller 无法加载资源:服务器响应状态为 500(内部服务器错误)Azure MVC - Failed to load resource: the server responded with a status of 500 (Internal Server Error) Azure MVC 加载资源失败:服务器响应状态为500(内部服务器错误)asp.net - Failed to load resource: the server responded with a status of 500 (Internal Server Error) asp.net 无法加载资源:服务器使用Ajax响应状态为500 - Failed to load resource: the server responded with a status of 500 using Ajax Ajax 调用资源加载失败:服务器响应状态为 500(内部服务器错误) - Ajax call with a failed to load resource: the server response with a status 500 (internal server error) 加载资源失败:服务器响应状态为 500(内部服务器错误) - failed to load resource: the server response with a status 500 (internal server error) 从 ajax 调用 WCF 并收到此错误,加载失败:服务器响应状态为 500 (System.ServiceModel.ServiceActivationException) - calling WCF from ajax and getting this error,Failed to load:server responded with a status of 500 (System.ServiceModel.ServiceActivationException) 错误:无法加载资源:服务器的状态为400(错误请求) - Error:Failed to load resource: the server responded with a status of 400 (Bad Request)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM