Ajax Request returns HTTP error 500, using MVC and

2019-03-11 21:04发布

I've seen several threads about this, and I've tried all the answers (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.

Here is my ajax call:

$.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:

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

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

The Form Data is being shown as :

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

newParentItemId:null

newPreviousItemId:null

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

Same error.

4条回答
做自己的国王
2楼-- · 2019-03-11 21:50

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

查看更多
你好瞎i
3楼-- · 2019-03-11 21:51

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.

查看更多
等我变得足够好
4楼-- · 2019-03-11 21:53

Fiddler is 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.

查看更多
孤傲高冷的网名
5楼-- · 2019-03-11 21:58

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 .cshtml file was not being published onto the server (wasnt "included" in the Visual Studio project when publishing).

查看更多
登录 后发表回答