jQuery 从 ASP.NET MVC 返回 Json 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3908605/
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
return Json error from ASP.NET MVC
提问by Dean
I'm trying to return an error message via Json from ASP.NET MVC controller. I want to display carriage returns on the screen, so the error will look like:
我正在尝试通过 Json 从 ASP.NET MVC 控制器返回错误消息。我想在屏幕上显示回车,所以错误看起来像:
Error 1.
Error 2.
错误 1.
错误 2。
instead of "Error1.\u003cbr/\u003eErro2.\u003cbr.\u003e"
代替 "Error1.\u003cbr/\u003eErro2.\u003cbr.\u003e"
Here's my ASP.NET MVC code
这是我的 ASP.NET MVC 代码
Response.StatusCode = (int)HttpStatusCode.BadRequest;
string str = "Error 1.<br/>Error 2.<br.>";
return Json(str);
JavaScript (redacted):
JavaScript(已编辑):
.ajax({...
error: function(xhr, textStatus, exceptionThrown) {
$('#result').html(xhr.responseText);
},
Debugging the xhr.responseText yields: ""Error1.\u003cbr/\u003eErro2.\u003cbr.\u003e""
调试 xhr.responseText 产生: ""Error1.\u003cbr/\u003eErro2.\u003cbr.\u003e""
Any ideas?
有任何想法吗?
回答by Avi Pinto
would be nicer to return a list of errors and then build the html at the client.
返回错误列表然后在客户端构建 html 会更好。
Response.StatusCode = (int)HttpStatusCode.BadRequest;
List<string> errors = new List<string>();
//..some processing
errors.Add("Error 1");
//..some processing
errors.Add("Error 2");
return Json(errors);
and then at the client side
然后在客户端
.ajax({...
error: function(xhr, textStatus, exceptionThrown) {
var errorData = $.parseJSON(xhr.responseText);
var errorMessages = [];
//this ugly loop is because List<> is serialized to an object instead of an array
for (var key in errorData)
{
errorMessages.push(errorData[key]);
}
$('#result').html(errorMessages.join("<br />"));
},
you can also return a more specific error object and use a template solution, but this is the idea
您还可以返回更具体的错误对象并使用模板解决方案,但这就是想法
回答by Hector Correa
If you are using JSON you probably don't want to embed HTML (that sort of defeats the purpose of returning a "data" object that you will format on the view.)
如果您使用的是 JSON,您可能不想嵌入 HTML(这违背了返回将在视图上格式化的“数据”对象的目的。)
If you want to return HTML you could do this in your controller:
如果您想返回 HTML,您可以在控制器中执行此操作:
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Content("Error 1.<br/>Error 2.<br/>");
回答by Oleg
It seems to me that you already made almost everything correct. The results which you have in the xhr.responseText
is a JSON string. So you should insert only one additional call of the JSON.parse
function
在我看来,你已经把几乎所有的事情都做对了。您在 中的结果xhr.responseText
是一个 JSON 字符串。所以你应该只插入一个额外的JSON.parse
函数调用
.ajax({...
error: function(xhr, textStatus, exceptionThrown) {
$('#result').html(JSON.parse(xhr.responseText));
},
then the data like '"Error1.\u003cbr/\u003eErro2.\u003cbr.\u003e"'
will be converted to the string 'Error 1.<br/>Error 2.<br/>'
.
然后像这样的数据'"Error1.\u003cbr/\u003eErro2.\u003cbr.\u003e"'
将被转换为字符串'Error 1.<br/>Error 2.<br/>'
。
Inside of success
event handler the ajax
function call JSON.parse
for you, but it do this notinside of the error
handler. So converting the server response from JSON string you have to do manually.
在success
事件处理程序内部为您ajax
调用函数JSON.parse
,但它不在error
处理程序内部执行此操作。因此,您必须手动从 JSON 字符串转换服务器响应。
回答by Bryan Migliorisi
Yea, you're defeating the purpose like @Hector said..
是的,你正在像@Hector 所说的那样违背目的。
And if you want to return true JSON, maybe try returning a Json(Dictionary<String, Object>)
instead of a Json(string)
.
如果您想返回真正的 JSON,可以尝试返回 aJson(Dictionary<String, Object>)
而不是 a Json(string)
。