views:

459

answers:

4

Here is my ajax call:

response = $.ajax({
            type: "POST",
            url: "/Row/getRowName",
            dataType: "json",
            data:({ currRow : rowName, offset : 5 }),
            error:function(request){alert(request.statusText)},
            success:function(result){alert(result)}
        }).responseText;

I get Internal Server Error as an alert. I also have a breakpoint set in my method getRowName in my RowController.

Here's the getRowName method:

    [AcceptVerbs(HttpVerbs.Post)]
    public string getRowName(string currRow, int offset)
    {
        string rowName;

        rowName = _rowRepository.getRowNameByOffset(currRow, offset);

        return rowName;
    }

What causes an Internal Server Error? How can I fix it? Am I using the whole controller concept properly or should I be using WebServices for things like this?

Edit: The controller that calls the page is HomeController where as the call I'm making is to RowController. RowController is in the same folder as HomeController (not sure if that matters).

But I found out if I create a mock function:

    [AcceptVerbs(HttpVerbs.Post)]
    public string sayHi()
    {
        return "Hi";
    }

and calling it the same way (different url since it's in the home controller):

response = $.ajax({
            type: "POST",
            url: "/Home/sayHi",
            dataType: "json",
            error:function(request){alert(request.statusText)},
            success:function(result){alert(result)}
        }).responseText;

Any ideas as to why this would work and the other wouldn't?

A: 

Your server should be resilient. Nothing JQuery or any client does should be able to cause a HTTP 500 error. As for the server, the code you've given explains nothing. It boils down to:

return _rowRepository.getRowNameByOffset(currRow, offset)

So we would probably need at least the source of getRowNameByOffset to start figuring it out.

EDIT: Another think you can try for testing is just having getRowName return something trivial like:

return "currRow" + " " + offset

to make sure the problem isn't elsewhere.

Matthew Flaschen
I just tried it and it didn't work, still getting the internal server error...
Matt
I'm new to MVC so I might have missed something obvious... I don't think so though
Matt
Uh...if your MVC app throws an unhandled exception, it's an automatic 500 response.
womp
womp, and if you're throwing unhandled exceptions that generally indicates a bug in your app.
Matthew Flaschen
A: 

I think your problem might be the parentheses around the data. It really should just be an object that is being used for the arguments. If I'm right, then the internal server error is because the route handler can't find a suitable signature for getRowName.

For it to work, you also need to return a JsonResult from your method, not a string. This could also be the issue since it may only look for methods that return an ActionResult.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult getRowName(string currRow, int offset)
{
    string rowName;

    rowName = _rowRepository.getRowNameByOffset(currRow, offset);

    return Json( new { RowName = rowName } );
}

Also, the Ajax call won't result in an object. You need to have your success handler do whatever it is you are trying to do with the response.

$.ajax({
        type: "POST",
        url: "/Row/getRowName",
        dataType: "json",
        data: { currRow : rowName, offset : 5 },
        error:function(request){alert(request.statusText)},
        success:function(result){
           $('#rowNameContainer').html( result.RowName );
        }
});

If you really need the result in a variable (not likely, but possible), then you could run the ajax call synchronously (async: false) and set a variable in the success function.

$(...).click( function() {
   var response = null;
   $.ajax({
        type: "POST",
        async: false,
        url: "/Row/getRowName",
        dataType: "json",
        data: { currRow : rowName, offset : 5 },
        error:function(request){alert(request.statusText)},
        success:function(result){
           response = result.RowName;
        }
   });

   ... do something with response...
   return false;
});
tvanfosson
The parentheses are not the problem. ({ currRow : rowName, offset : 5 }) and { currRow : rowName, offset : 5 } evaluate to the same thing here (an object with 2 keys). Try it. However, you are right about .ajax being async.
Matthew Flaschen
+2  A: 

Are you running IE8 or Firefox with FireBug installed? Using the script consoles in either will give you a lot more information on the details of the error you're getting from the server.

The differences between the two methods you post are the parameters - the second method in your edit doesn't have any, while the one in the first method has some - I'd take a fairly good bet that the error you're seeing is to do with the framework not deserialising your data correctly.

In the first instance, have you tried:

data:"{ \"currRow\":\"" + rowName + "\", \"offset\":\"5\" }"

Alternatively, you could take a look at JSON2 to do your data serialisation - there's a nice article on it here:

Using complex types to make calling services less complex

Zhaph - Ben Duguid
A: 

public ActionResult sayHi()

Rony