.net - json returned from asmx method can't be parsed in Javascript -
i trying return json object of array of arrays one:
{ 'data': [[45,43,103],[34,43,230]] }
using asmx in .net 4.0 this:
[webmethod] [scriptmethod(responseformat = responseformat.json)] public string getdata() { stringbuilder sb = new stringbuilder(); sb.append("'data':["); sb.append("[45,43,103],"); sb.append("[34,43,230]"); sb.append("]"); return sb.tostring();
using jquery ajax call :
$.ajax({ type: "post", url: url, //defined elsewhere data: "{}", datatype: 'json', contenttype: "application/json; charset=utf-8", success: update, });
and
function update(data) { console.log (data.d[1][1]); //looking @ second array second element }
the problem response asmx call looks , update function doesn't work
{"d":"\u0027data\u0027:[[45,43,50],[34,43,50]]"}
things don't escaped or formatted properly. seems missing something. why 'data' still have unicode chars '? thought responseformat.json take care of put in json format.
you don't return json containing array of arrays. return string ('data':[[45,43,50],[34,43,50]]
) encapsulated in json.
2 things wrong approach.
a) don't form json manually using string operations
b) return real object in method instead of string
so changing method below should work
[webmethod] [scriptmethod(responseformat = responseformat.json)] public list<int[]> getdata() { return new list<int[]>() { new[] { 45, 43, 103 }, new[] { 34, 43, 230 } }; }
Comments
Post a Comment