c# - Not able to access JSON datas -
i trying loop through json object , create input type dynamically in div
here jquery function
$(document).ready(function (){ $.ajax({ type: "post", url: "tables.aspx/totatable", contenttype: "application/json; charset=utf-8", datatype:'json', success: function(data) { //var data= $.parsejson(data); $.each(data,function(index,jsonobj) { alert(jsonobj.otid); } ); } });
});
here server side code return json values using json.net framework convert datatable json
[system.web.services.webmethod(enablesession=true)] public static string totatable() { logindata ldata = (logindata)httpcontext.current.session["ldata"]; clsdataaccess cdata = new clsdataaccess(); datatable dt = cdata.gettable("select otid,ottableno fboutlettables otoutletid=1"); string val=jsonconvert.serializeobject(dt).tostring(); return val; }
here json value sent client side
[{"otid":76.0,"ottableno":222.0},{"otid":3.0,"ottableno":3.0},{"otid":4.0,"ottableno":4.0},{"otid":5.0,"ottableno":5.0},{"otid":6.0,"ottableno":6.0},{"otid":7.0,"ottableno":7.0},{"otid":8.0,"ottableno":8.0},{"otid":9.0,"ottableno":9.0},{"otid":2.0,"ottableno":2.0},{"otid":1.0,"ottableno":1.0}]
my problem alert(jsonobj.otid);
shows 'undefined'
i did try changing server side function return list
like
public static list<tables> tables() { list<tables> mylist=new list<tables>(); //code datas database foreach(datarow row in dt.rows) { mylist.add(new tables{ tabid=row["tabid"].tostring(); tables=row["tab"].tostring(); }); } return mylist; }
but facing same issue..
finally got working
here jquery code
var obj={}; obj.id=id; $.ajax({ type:"post", url:"tables.aspx/itemsfromcategory", data: json.stringify(obj), contenttype: "application/json; charset=utf-8", datatype:'json' }).done(function(res) { var resultasjson = res.d; $.each(resultasjson, function (index, resobject) { alert(resobject.id); }); } });
i did change server side code this
public static list<itemclass> itemsfromcategory(string id) { logindata ldata = (logindata)httpcontext.current.session["ldata"]; system.web.script.serialization.javascriptserializer ser = new system.web.script.serialization.javascriptserializer(); list<itemclass> mylist = new list<itemclass>(); if (ldata != null) { clsdataaccess cdata = new clsdataaccess(); datatable dt = cdata.gettable("select itmid,itmname fbitemdetails itmtype='i' , itmisprimary='n' , itmoutletid=" + ldata.fboutletid + " , itmgrpid=" + id); // datatable dt = cdata.gettable("select itmid,itmname fbitemdetails itmtype='i' , itmisprimary='n' , itmgrpid=" + id); foreach (datarow row in dt.rows) { mylist.add(new itemclass { id = row["itmid"].tostring(), itemname=row["itmname"].tostring(), }); } // result = ser.serialize(mylist); // return result; return mylist; } // return string.empty; return mylist; }
Comments
Post a Comment