当我从 ASP.NET MVC 项目上的 jquery $.ajax 调用返回“内部服务器错误”时有什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1036109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is wrong when I get "Internal Server Error" returned from a jquery $.ajax call on an ASP.NET MVC project?
提问by Matt
Here is my ajax call:
这是我的ajax调用:
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.
我收到内部服务器错误作为警报。我还在我的 RowController 的 getRowName 方法中设置了一个断点。
Here's the getRowName method:
这是 getRowName 方法:
[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?
什么导致内部服务器错误?我该如何解决?我是否正确使用了整个控制器概念,还是应该将 WebServices 用于此类事情?
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).
编辑:调用页面的控制器是 HomeController,因为我正在调用的是 RowController。RowController 与 HomeController 位于同一文件夹中(不确定是否重要)。
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?
关于为什么这会起作用而另一个不起作用的任何想法?
回答by Zhaph - Ben Duguid
Are you running IE8 or Firefox with FireBuginstalled? 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.
您运行的是安装了FireBug 的IE8 还是 Firefox ?在任一中使用脚本控制台将为您提供有关从服务器获得的错误详细信息的更多信息。
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\" }"
回答by Matthew Flaschen
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:
您的服务器应该具有弹性。JQuery 或任何客户端都不会导致 HTTP 500 错误。至于服务器,您提供的代码没有任何解释。归结为:
return _rowRepository.getRowNameByOffset(currRow, offset)
So we would probably need at least the source of getRowNameByOffset to start figuring it out.
所以我们可能至少需要 getRowNameByOffset 的来源才能开始弄清楚。
EDIT: Another think you can try for testing is just having getRowName return something trivial like:
编辑:您可以尝试进行测试的另一个想法是让 getRowName 返回一些微不足道的东西,例如:
return "currRow" + " " + offset
to make sure the problem isn't elsewhere.
以确保问题不在其他地方。
回答by tvanfosson
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
.
我认为您的问题可能是数据周围的括号。它真的应该只是一个用于参数的对象。如果我是对的,那么内部服务器错误是因为路由处理程序找不到合适的签名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.
要使其正常工作,您还需要从您的方法中返回一个 JsonResult,而不是一个字符串。这也可能是问题,因为它可能只查找返回 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 调用不会产生对象。您需要让您的成功处理程序执行您试图对响应执行的任何操作。
$.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.
如果您确实需要变量中的结果(不太可能,但可能),那么您可以同步运行 ajax 调用 ( async: false
) 并在成功函数中设置一个变量。
$(...).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;
});
回答by Greg
I got a 500 error because I didn't explicitly add this:
我收到了 500 错误,因为我没有明确添加:
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.
一旦我添加了它,一切都很好。我没有明确地将 Action 方法标记为 GET 或 POST,所以我假设默认情况下它是 POST - 因此出现错误。
回答by RationalRabbit
You might also check your file permissions. You will also receive this message if your server is set more restrictive than your file.
您还可以检查您的文件权限。如果您的服务器设置比您的文件更严格,您也会收到此消息。