Reputation: 5459
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?
Upvotes: 2
Views: 21782
Reputation: 1067
You might also check your file permissions. You will also receive this message if your server is set more restrictive than your file.
Upvotes: 0
Reputation: 26976
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\" }"
Upvotes: 3
Reputation: 811
I got a 500 error because I didn't explicitly add this:
type: "POST",
Once I added that, everything was fine. I didn't explicitly mark the Action method as GET or POST, so I assume by default it's POST - hence the error.
Upvotes: 0
Reputation: 532695
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;
});
Upvotes: 0
Reputation: 285037
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.
Upvotes: 0