从 ASP.NET MVC 操作返回什么以允许触发 jQuery ajax 成功事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2208658/
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 to return from ASP.NET MVC action to allow jQuery ajax success event to fire?
提问by Sailing Judo
Right now my ajax posts all fire their Error events even if the action did not cause an error. I have an ASP.NET MVC action that looks like:
现在,即使操作没有导致错误,我的 ajax 帖子也会触发它们的 Error 事件。我有一个 ASP.NET MVC 操作,如下所示:
[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult StuffToDo(int id)
{
// do various things that work ...
return new JsonResult(); // i have to return something, so this...
}
On the client side I have this jQuery:
在客户端,我有这个 jQuery:
$('#actionClick').click(function() {
if (confirm('Are you sure?')) {
$.ajax({
type: "POST",
url: "/Customer/StuffToDo/<%= Model.Customer.Id %>",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: function() {
ShowSuccessResult("Yay!");
},
error: function(xhr, ajaxOptions, thrownError) {
ShowErrorResult("Boo! Message:" + xhr.responseText);
}
});
}
return false;
});
If the action is successful (no exceptions thrown) then I would expect the success event handler to be triggered. Instead the error event is firing. Is there something I should pass back or change in the action so the success event fires?
如果操作成功(没有抛出异常),那么我希望触发成功事件处理程序。相反,错误事件正在触发。有什么我应该回传或更改操作以便成功事件触发的吗?
I realize this question is basically the same as this other question, but my error handler already has the expanded signature which solved the other person's question.
我意识到这个问题与另一个问题基本相同,但是我的错误处理程序已经有了解决其他人问题的扩展签名。
I changed the return value to Null to see if that affected anything, but no behavior change.
我将返回值更改为 Null 以查看是否有任何影响,但行为没有改变。
Its starting to look like this is an issue with HTTPS. I get multiple responses from the request. The first 2 are 401 messages and then I get a 200.
它开始看起来像是 HTTPS 的问题。我从请求中得到多个响应。前 2 条是 401 条消息,然后我收到 200 条消息。
回答by 48klocs
I believe the problem is that you're not returning anything in the JsonResult. Try:
我相信问题在于您没有在 JsonResult 中返回任何内容。尝试:
return this.Json(string.Empty);
and see if that works. I think that the problem is that you're returning nothing to the jQuery call rather than an empty JSON set:
看看这是否有效。我认为问题在于您没有向 jQuery 调用返回任何内容,而不是一个空的 JSON 集:
{}
回答by Daniel Little
Returning an empty result may also be useful
返回空结果也可能有用
return new EmptyResult();
Note: Your return type doesn't need to be ActionResult or event anything that inherits from it. In fact mvc will let you call any public method. However you should always use the most explicit return type you want just like any other method (like JsonResult).
注意:您的返回类型不需要是 ActionResult 或从它继承的任何事件。事实上 mvc 会让你调用任何公共方法。但是,您应该始终使用您想要的最明确的返回类型,就像任何其他方法(如 JsonResult)一样。
回答by StuartLC
2 quick notes for anyone landing here struggling to trigger the ajax jQuery success
callback from an MVC controller:
为任何登陆这里而努力success
从 MVC 控制器触发 ajax jQuery回调的人提供 2 个快速说明:
- If you're using
type: "GET"
in your Ajax, you'll need to includeJsonRequestBehavior.AllowGet
to bypass the default anti JSON HiHymaning security measures
- 如果您
type: "GET"
在 Ajax 中使用,则需要包含JsonRequestBehavior.AllowGet
以绕过默认的反JSON 劫持安全措施
return Json(true, JsonRequestBehavior.AllowGet);
- As per above, if you've indicated
dataType: 'json'
in the Ajax request, if you are making a one-way call (e.g.Save
) where you don't need anything back, you can return just about anything serializable asJson
from the controller. But with a 200, something must be returned. Use a 204 if no data is returned.
- 如上所述,如果您已
dataType: 'json'
在 Ajax 请求中指出,如果您在Save
不需要任何返回的情况下进行单向调用(例如),您可以Json
从控制器返回几乎任何可序列化的内容。但是对于 200,必须返回一些东西。如果没有返回数据,请使用 204。
Wrong - Triggers the Ajax error
handler !
错误 - 触发 Ajaxerror
处理程序!
// Nope - Status 200 must have some content
return new HttpStatusCodeResult(HttpStatusCode.OK);
// Also returns a 200 with no data - not allowed
return new EmptyResult();
Works - triggers the Ajax success handler:
有效 - 触发 Ajax 成功处理程序:
// Return a 204 if there's no data
return new HttpStatusCodeResult(HttpStatusCode.NoContent);
// Return something with a 200 return
return Json(new {Success = true});
回答by ChrisP
The return type on your method should be JsonResult instead of ActionResult.
您的方法的返回类型应该是 JsonResult 而不是 ActionResult。