Asp.Net MVC3,返回成功 JsonResult [英] Asp.Net MVC3, returning success JsonResult
本文介绍了Asp.Net MVC3,返回成功 JsonResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我还需要返回包含成功值(true 或 false)的 JSON 数据,它还需要有结果消息.
I need to return JSON data that contain success value (true or false) also, it need to have result message too.
所以我使用 Dictionary 来包含数据,但是当它返回到 Jason 数据时,它包含 ""(Quot).
so I using Dictionary to contain data but when it return to Jason data, it contain ""(Quot).
JsonResult = new Dictionary<string, string>();
JsonResult.Add("Success", "False");
JsonResult.Add("Message", "Error Message");
return Json(JsonResult);
它返回,
{"Success":"False","Message":"Error Message"}
但我需要,
{Success:False,Message:"Error Message"} //with out "" (Quot)
有人知道吗?
谢谢!
推荐答案
{"Success":"False","Message":"Error Message"}
是有效的 JSON.您可以在此处查看.在 jsonlint.com
is valid JSON. You can check it here. in jsonlint.com
您甚至不需要字典来返回该 JSON.您可以简单地使用这样的匿名变量:
You don't even need a Dictionary to return that JSON. You can simply use an anonymous variable like this:
public ActionResult YourActionMethodName()
{
var result=new { Success="False", Message="Error Message"};
return Json(result, JsonRequestBehavior.AllowGet);
}
要从您的客户访问此数据,您可以这样做.
to Access this data from your client, you can do this.
$(function(){
$.getJSON('YourController/YourActionMethodName', function(data) {
alert(data.Success);
alert(data.Message);
});
});
这篇关于Asp.Net MVC3,返回成功 JsonResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文