jQuery Ajax 请求返回 HTTP 错误 500,使用 MVC 和 $.ajax 调用与 POST?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13942304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:07:03  来源:igfitidea点击:

Ajax Request returns HTTP error 500, using MVC and $.ajax call with POST?

asp.net-mvcjqueryasp.net-ajax

提问by SventoryMang

I've seen several threads about this, and I've tried all the answers (ASP.NET MVC JsonResult return 500)

我已经看到了几个关于这个的线程,我已经尝试了所有的答案(ASP.NET MVC JsonResult return 500

My ajax request is re-turning a 500 Internal Error. If I debug I never even get to my action.

我的 ajax 请求返回 500 内部错误。如果我进行调试,我什至不会采取行动。

Here is my ajax call:

这是我的ajax调用:

$.ajax({
                    url: '@Url.Action("UpdateSortOrder", "FormItems")',
                    data: { itemToUpdateId: item.attr("id"), newParentItemId: parentItemId, newPreviousItemId: previousItemId },
                    type: 'POST',
                    success: function (data) {
                        console.log(data);
                    },
                    error: function (xhr, status, exception) {
                        console.log("Error: " + exception + ", Status: " + status);
                    }
                });

And my action:

而我的行动:

[HttpPost]
    public ActionResult UpdateSortOrder(Guid itemToUpdateId, Guid newParentItemId, Guid newPreviousItemId)
    {
        FormItem updatedItem = _formItemService.GetOne(x => x.Id == itemToUpdateId);

        return Json(updatedItem, JsonRequestBehavior.DenyGet);
    }

Using chrome console, these are the response headers from the reply:

使用 chrome 控制台,这些是来自回复的响应头:

HTTP/1.1 500 Internal Server Error Cache-Control: private Content-Type: text/html; charset=utf-8 Server: Microsoft-IIS/7.5 X-AspNetMvc-Version: 3.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Tue, 18 Dec 2012 21:53:41 GMT Content-Length: 17041

HTTP/1.1 500 内部服务器错误缓存控制:私有内容类型:text/html;charset=utf-8 服务器:Microsoft-IIS/7.5 X-AspNetMvc-版本:3.0 X-AspNet-版本:4.0.30319 X-Powered-By:ASP.NET 日期:2012 年 12 月 18 日星期二 21:53:41 GMT内容长度:17041

Server logs show no substatus codes. Any idea what I am doing wrong here? I've prefer to use POST instead of GET.

服务器日志不显示子状态代码。知道我在这里做错了什么吗?我更喜欢使用 POST 而不是 GET。

The Form Data is being shown as :

表单数据显示为:

itemToUpdateId:18ac5399-342e-4a39-9da1-3281a89501df

newParentItemId:null

newPreviousItemId:null

itemToUpdateId:18ac5399-342e-4a39-9da1-3281a89501df

新的父项 ID:空

newPreviousItemId:空

Which is correct.

哪个是正确的。

I've tried setting the contentType to application/json and traditional = true like in this question: Sending ajax post to mvc with "application/json; charset=utf-8" returns error 500 from vs web developer server

我已经尝试将 contentType 设置为 application/json 和传统 = true 就像在这个问题中一样:Sending ajax post to mvc with "application/json; charset=utf-8" returns error 500 from vs web developer server

Same error.

同样的错误。

回答by SventoryMang

Well I was able to figure out what the problem was, there was nothing wrong with my AJAX syntax, or even the action. It was just that my returned object contained a circular reference. I was able to see the actual error in chrome console by clicking on the POST request under Network tab, then viewing the preview tab. This displayed the actual error message.

好吧,我能够找出问题所在,我的 AJAX 语法甚至操作都没有问题。只是我返回的对象包含一个循环引用。通过单击“网络”选项卡下的 POST 请求,然后查看“预览”选项卡,我能够在 chrome 控制台中看到实际错误。这显示了实际的错误消息。

回答by Chris Hammond

Old post, alternative answer ... Just in case someone ends up here ..

旧帖子,替代答案......以防万一有人在这里结束......

My issue was caused by the fact that my controller action that I was calling returned a Partial View Action Result and the PartialView .cshtmlfile was not being published onto the server (wasnt "included" in the Visual Studio project when publishing).

我的问题是由于我调用的控制器操作返回了 Partial View Action Result 并且 PartialView.cshtml文件没有发布到服务器上(发布时没有“包含”在 Visual Studio 项目中)。

回答by Ibrahim Amer

Fiddleris a great tool for catching http requests which is quite useful for debugging responses between client side and server side. Just open it when you are suspecting the problem in your browser, and when the error occur open fiddler and choose the request, then view its raw information.

Fiddler是一个很好的捕获 http 请求的工具,它对于调试客户端和服务器端之间的响应非常有用。当你怀疑浏览器有问题时打开它,当出现错误时打开 fiddler 并选择请求,然后查看其原始信息。

回答by RafiO

if you do not need updatedItem in success then do not return it just return a simple variable

如果您不需要 updateItem 成功则不要返回它只返回一个简单的变量