[英]ajax call serialize and deserialize data
I have the following ajax call: 我有以下ajax调用:
$(document).ready(function() {
$.ajax({
type: "POST",
url: "/LoadCount.aspx/GetCounts",
contentType: "application/json; charset=utf-8",
data: "",
async: false,
dataType: "json",
success: function(data) {
var myObj = JSON.parse(data.d);
alert(myObj.length);
for (var i = 0; i < myObj.length; i++) {
alert(myObj[0]);
}
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
});
I am getting undefined when I alert the length 当我提醒长度时,我得到了不确定
Here's my Method in my LoadCount.aspx 这是我的LoadCount.aspx中的方法
[WebMethod]
public static string ObtenerContador()
{
List<MenuItem> menu =(List<MenuItem>)HttpContext.Current.Session["MenuItems"];
Dictionary<string, int> dic = new Dictionary<string, int>();
foreach (MenuItemAMostrar item in menu)
{
dic.Add(item.ControlID,1);
};
return new JavaScriptSerializer().Serialize(dic); //or any other suggested serialization method
}
this is what I am getting in the success function: 这就是我在成功函数中得到的结果:
{"MenuCLESAAsignar":1,"MenuOCGestores":1,"MenuOcAAutorizar":1,"MenuOcAAutorizarBanco":1,"MenuOCGestoresObservadas":1,"MenuCLESActivos":1,"MenuParametros":1,"MenuCLESConAddendaAAprobarSup":1,"MenuPendienteAprobacion":1,"MenuConsultaCLES":1}
The question is, how do I deserialize that dictionary in my ajax success function? 问题是,如何在我的ajax成功函数中反序列化该字典? Once deserialized, how do I populate it, obtaining the data?
一旦反序列化,我如何填充它,获取数据?
thanks! 谢谢!
Are you looking for this? 你在找这个吗?
success: function (data)
{
var myObj = JSON.parse(data.d);
},
As somebody else mentioned, you just use JSON.parse()
to parse the data. 正如其他人提到的,你只需使用
JSON.parse()
来解析数据。
From there it just creates an object, and you can access the data with .
从那里它只创建一个对象,您可以使用它来访问数据
.
. 。
Like so: 像这样:
http://jsfiddle.net/SJGLF/1/ http://jsfiddle.net/SJGLF/1/
var myData = JSON.parse(jsonData);
for(var i in myData)
{
console.log(i + " = " + myData[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.