I want to access json data into jQuery returning from Asp.Net Web Method. This is my web Method
[WebMethod(EnableSession=true)]
public string GetChat() {
//
// Code to get data into dataset from database
//
// below code to convert dataset to json string
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in ds.Tables[0].Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn dc in ds.Tables[0].Columns)
{
row.Add(dc.ColumnName.Trim(), dr[dc]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
Here is my jQuery Code.
$.ajax({
type: "POST",
contentType: "application/json",
url: "../chat.asmx/GetChat",
dataType: "json",
success: function (data) {
alert(data.d[0].Name);
},
error: function (result) {
alert("Error");
}
});
Here is Json Response Sample:
[
{
"EmployeeId":3,
"Name":"Khushbu Agarwal",
"Cnt":2
},
{
"EmployeeId":6,
"Name":"Priyanka Jain",
"Cnt":3
}
]
Now, When I access data in jQuery by statement:
alert(data.d[0].Name);
It displays Undefined
Can any one help me on this. I am new to json.
Thanks in advance.
undefined. and I am already using and tracing Javascript and AJAX by Firebug Console.