Thursday, 15 May 2014

jquery - Failed to load resource: the server responded with a status of 500 (Internal Server Error) When using JqGrid -



jquery - Failed to load resource: the server responded with a status of 500 (Internal Server Error) When using JqGrid -

i getting problem server returns 500 error. same url running on when utilize within ajax call

url = "testservice.asmx/getqueryinfo"

jquery("#list2").jqgrid({ mtype: 'post', postdata: "{ tablename: '" + tablename + "', columnlist: '" + columnnames + "' }", serializegriddata: function (postdata) { // extend parameter send server postdata = json.stringify(postdata); // serialize parameters json string alert(postdata); homecoming json.stringify(postdata); }, url: "predictivedialer.asmx/getqueryinfo", ajaxgridoptions: { contenttype: 'application/json; charset=utf-8' }, datatype: 'json', colnames: ['id', 'code', 'name', 'password', 'clientlevel', 'deptno', 'deptname'], colmodel: [ { name: 'id', index: 'id', width: 55 }, { name: 'code', index: 'code', width: 90 }, { name: 'name', index: 'name', width: 100 }, { name: 'password', index: 'password', width: 80, align: "right" }, { name: 'clientlevel', index: 'clientlevel', width: 80, align: "right" }, { name: 'deptno', index: 'deptno', width: 80, align: "right" }, { name: 'deptname', index: 'deptname', width: 150, sortable: false } ], rownum: 10, rowlist: [10, 20, 30], pager: '#pager2', sortname: 'id', viewrecords: true, sortorder: "id", caption: "json example", jsonreader: { root: function (obj) { homecoming obj.d; }, page: function () { homecoming 1; }, total: function () { homecoming 1; }, records: function (obj) { alert(obj.d.length); homecoming obj.d.length; } } }); jquery("#list2").jqgrid('navgrid', '#pager2', { edit: false, add: false, del: false });

and webservice code below

[webmethod] public string getqueryinfo(string tablename, string columnlist) { string daresult = string.empty; string sql = string.empty; sqlconnection con = new sqlconnection("myconnectionstring"); sql = "select " + columnlist + " "+ tablename; sqldataadapter sda = new sqldataadapter(sql, con); dataset ds = new dataset(); sda.fill(ds); daresult = datasettojson(ds); homecoming daresult; } public string datasettojson(dataset ds) { dictionary<string, object> dict = new dictionary<string, object>(); foreach (datatable dt in ds.tables) { object[] arr = new object[dt.rows.count + 1]; (int = 0; <= dt.rows.count - 1; i++) { arr[i] = dt.rows[i].itemarray; } dict.add(dt.tablename, arr); } javascriptserializer json = new javascriptserializer(); homecoming json.serialize(dict); }

is wrong above code?

updated code

now used new function paise columnname columnvalue inarray

server side :

[webmethod] public object getqueryinfo(string tablenames, string columnlist) { object daresult = new object(); string sql = string.empty; sqlconnection con = new sqlconnection("myconnection"); sql = "select top 2 " + columnlist + " " + tablenames; sqldataadapter sda = new sqldataadapter(sql, con); dataset ds = new dataset(); sda.fill(ds); daresult = datatabletojson(ds.tables[0]); //daresult = datasettojson(ds); homecoming daresult; } public object datatabletojson(datatable table) { list<dictionary<string, object>> list = new list<dictionary<string, object>>(); foreach (datarow row in table.rows) { dictionary<string, object> dict = new dictionary<string, object>(); foreach (datacolumn col in table.columns) { dict[col.columnname.tolower()] = row[col]; } list.add(dict); } homecoming list; }

client side :

jquery("#list2").jqgrid({ mtype: 'post', url: "myservice.asmx/getqueryinfo", serializegriddata: function (postdata) { homecoming json.stringify({ tablenames: tablename, columnlist: columnnames }); }, ajaxgridoptions: { contenttype: "application/json; //charset=utf-8" }, //charset=utf-8 datatype: 'json', colnames: ['id', 'code', 'name', 'password', 'clientlevel', 'deptno', 'deptname'], colmodel: [ { name: 'id', index: "id", width: 55, key: true, localreader: { id: "id" } }, { name: 'code', width: 90, align: 'left' }, { name: 'name', width: 100, align: 'left' }, { name: 'password', width: 80 }, { name: 'clientlevel', width: 80 }, { name: 'deptno', width: 80 }, { name: 'deptname', width: 150 } ], jsonreader: { repeatitems: false, root:'d', page: function (obj) { homecoming 1; }, total: function (obj) { homecoming 1; }, records: function (obj) { homecoming obj.length; }, id : "id" }, autoencode: true, gridview: true, rownum: 10, loadonce: true, rowlist: [10, 20, 30], pager: '#pager2', viewrecords: true, caption: "json example", loaderror: function (jqxhr, textstatus, errorthrown) { alert('http status code: ' + jqxhr.status + '\n' + 'textstatus: ' + textstatus + '\n' + 'errorthrown: ' + errorthrown); alert('http message body (jqxhr.responsetext): ' + '\n' + jqxhr.responsetext); } }); jquery("#list2").jqgrid('navgrid', '#pager2', { edit: false, add: false, del: false });

your current code have many problems. main is: convert info returned getqueryinfo explicitly json string wrong. method should homecoming object instead of string. .net framework convert returned object json string automatically you.

the sec problem wrong usage of postdata. should never seek convert returned object json manually (like "{ tablename: '" + tablename + "', columnlist: '" + columnnames + "' }"). instead of can remove postdata , modify code of serializegriddata like

class="lang-js prettyprint-override">serializegriddata: function () { homecoming json.stringify({ tablename: tablename, columnlist: columnnames }); }

it replace standard parameters send typically server tablename , columnlist.

you should never trust input info in server code. current implementation of getqueryinfo ideal way sql injection (see here illustration too). such code should never used in production.

the lastly remark. should add together options in jqgrid: loadonce: true (because don't implemented server side paging of data), gridview: true (to improve performance of grid), autoencode: true (to interpret input info text , not html fragments). recommend add together loaderror cellback grid (see the answer). display error message in case of server side errors 500.

jquery json jqgrid

No comments:

Post a Comment