SventoryMang
SventoryMang

Reputation: 10478

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

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.

Upvotes: 28

Views: 44276

Answers (4)

Chris Hammond
Chris Hammond

Reputation: 2186

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

Upvotes: 2

RafiO
RafiO

Reputation: 123

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

Upvotes: 0

Ibrahim Amer
Ibrahim Amer

Reputation: 1216

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.

Upvotes: 1

SventoryMang
SventoryMang

Reputation: 10478

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.

Upvotes: 80

Related Questions