28

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.

Community
  • 1
  • 1
SventoryMang
  • 10,275
  • 15
  • 70
  • 113
  • 3
    Have you tried viewing the raw POST using Fiddler to see what is going across the wire? – Biff MaGriff Dec 18 '12 at 22:18
  • 1
    can you share how you're setting your routes? – kabaros Dec 18 '12 at 22:19
  • The route is working, right now it just uses the default route rule populated by adding an area. If I change the action to return a view and type in the URL, it works, so the route is processing correctly. – SventoryMang Dec 18 '12 at 22:35
  • 1
    @BiffMaGriff I posted that from Chrome, but I figured it out now thanks to checking that window again, if I check the "preview" tab of the POST in chrome console, it gave me the actual error, which was the formItem being returned in the action was having a circular reference error! – SventoryMang Dec 18 '12 at 22:43

4 Answers4

80

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.

SventoryMang
  • 10,275
  • 15
  • 70
  • 113
  • 2
    Was able to troubleshoot similarly in IE11 Developer Tools using Network tab > Details > Response body. – Jason Marsell May 24 '16 at 20:32
  • For anyone else still having issues, check out this [solution](http://stackoverflow.com/a/16916600/4896260). It provides a more detailed error that Fiddler and such were not able to provide. – usr4896260 Mar 06 '17 at 15:17
  • 1
    3rd time I've Googled this gem of an answer - must commit to memory! (upvoted previously) – JsAndDotNet Apr 24 '19 at 15:43
  • In my case I was returning a View that didn't exist when I should have been returning JSON. Boneheaded mistake that throws a 500. If you use the network tab in any browser you'll clearly see the error from asp.net. – tnk479 Jun 11 '19 at 23:06
2

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).

Chris Hammond
  • 2,064
  • 5
  • 27
  • 53
1

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.

Ibrahim Amer
  • 1,147
  • 4
  • 19
  • 46
0

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

RafiO
  • 113
  • 1
  • 3
  • 9