Passing string array from ASP.NET to JavaScript -
i trying call server side javascript , pass string array javascript, running problems.
// call server-side data. $.ajax({"url" : "mywebpage.aspx/getdata", "type" : "post", "data" : {"iddata" : iddata}, "datatype" : "json", "success": function (data) { // data. var responsearray = json.parse(data.response); // extract header , body components. var strheader = responsearray[0]; var strbody = responsearray[1]; // set data on form. document.getelementbyid("divheader").innerhtml = strheader; document.getelementbyid("divbody").innerhtml = strbody; } });
on asp.net server side, have:
[webmethod] [scriptmethod(responseformat = responseformat.json)] public static object gettip(string idtip) { int iidtip = -1; string[] mydata = new string[2]; // formulate respnse. mydata[0] = "my header"; mydata[1] = "my body"; // create json object create response in format needed. javascriptserializer ojss = new javascriptserializer(); // create json response. string strresponse = ojss.serialize(mydata); return strresponse; }
i mixing things up, still new json.
update error code:
exception thrown @ line 2, column 10807 in http://localhost:49928/scripts/js/jquery-1.7.2.min.js
0x800a03f6 - javascript runtime error: invalid character
stack trace: parse json[jquery-1.7.2.min.js] line 2
what problem?
i modified ajax call script :
// call server-side data. $.ajax({ url: "webform4.aspx/gettip", type: "post", data: json.stringify({ idtip: "0" }), datatype: "json", contenttype: 'application/json', success: function (data) { // data. var responsearray = json.parse(data.d); // extract header , body components. var strheader = responsearray[0]; var strbody = responsearray[1]; // set data on form. document.getelementbyid("divheader").innerhtml = strheader; document.getelementbyid("divbody").innerhtml = strbody; } });
note added contenttype: 'application/json'
, changed
var responsearray = json.parse(data.response);
to
var responsearray = json.parse(data.d);
Comments
Post a Comment