2
Reply

Can not read JSON data.

Priyanka P

Priyanka P

Jul 5 2017 3:37 AM
204
I am creating application where i send data in form of ajax request and accept in form of JSON, i want to display that JSON data in gridview, but not succeded in it. when i try to print data in JSON(data.d.length) it display 272 (i think it is counting spaces also). I need help in reading data from JSON and bind it to GridView.
 
In the below way i am sending data to webmethod in ajax request
 
$.ajax({
type: "POST",
url: "StudentData.aspx/GetData",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
 
Below code works perfectly fine and send data in JSON format. 
 
[WebMethod]
//public static List<Student> GetData()
public static string GetData(string name)
{
 string constr = @"connection string";
string sql = "query";
SqlDataAdapter da = new SqlDataAdapter(sql, constr);
DataSet ds = new DataSet();
da.Fill(ds);
return JsonConvert.SerializeObject(ds, Newtonsoft.Json.Formatting.Indented);
 
//In below code when i print data.d.length it display 272. 
function OnSuccess(data) {
alert("In success");
alert(data.d.length);
 
 

Answers (2)