c# - unable to get response from a webservice method using json and ajax call -
i have created webservice , has 2 methods , when phone call first method working when phone call getpostings method unable response able phone call method
using system; using system.collections.generic; using system.linq; using system.web; using system.web.services; using system.data; using system.data.sqlclient; using system.web.script.services; using system.web.script.serialization; namespace simpleservice { public class errors { public int id { get; set; } public int info { get; set; } } /// <summary> /// summary description service1 /// </summary> [webservice(namespace = "http://tempuri.org/")] [webservicebinding(conformsto = wsiprofiles.basicprofile1_1)] [system.componentmodel.toolboxitem(false)] // allow web service called script, using asp.net ajax, uncomment next line. [system.web.script.services.scriptservice] public class service1 : system.web.services.webservice { [webmethod] public string getcurrenttime(string name) { homecoming "hello " + name + environment.newline + "the current time is: " + datetime.now.tostring(); } [webmethod] //[scriptmethod(usehttpget=true)] [scriptmethod(responseformat = responseformat.json,usehttpget = true)] public list<errors> getpostings() { string connetionstring = null; sqlconnection cnn; connetionstring = "data source=mycomputer.\\scom;initial catalog=portal;integrated security=true"; cnn = new sqlconnection(connetionstring); const string sql = "select id,openemr openemrdata"; sqlcommand mycommand = new sqlcommand(sql, cnn); cnn.open(); mycommand.executenonquery(); // myconnection.close(); sqldataadapter dataadapter = new sqldataadapter(); dataadapter.selectcommand = mycommand; dataset dset = new dataset(); dataadapter.fill(dset); javascriptserializer js = new javascriptserializer(); string strjson = js.serialize(jaggedarray); list<errors> errors = new list<errors>(); foreach (datarow row in dset.tables[0].rows) { errors jp = new errors(); jp.id = (int)row["id"]; jp.data = (int)row["openemr"]; //jp.id = (int)row["id"]; //jp.jobpost = row["jobposting"].tostring(); //jp.applicant = row["applicant"].tostring(); //jp.type = row["type"].tostring(); errors.add(jp); } homecoming errors; } }
}
here java script code
`<script type = "text/javascript"> function showcurrenttime() { alert(1); $.ajax({ type: "get", url: "~/service1.asmx/getpostings", data: '', contenttype: "application/json; charset=utf-8", datatype: "json", success: onsuccess, failure: function (response) { alert(1); alert(response.d); } }); } function onsuccess(response) { alert(1); var mydata = response.d; var data1; var data2; alert(suresh); alert(mydata.length + " hellos"); (var = 0; < mydata.length; i++) { $("#chart").append(mydata[i].id + " " + mydata[i].data); var list = mydata; } for(var i=0;i<list.length;i++) { $("#chart").append( list[i].openemr); }; } </script>`
use post instead of else decorate webmethode next attributes
[webmethod] [scriptmethod(usehttpget = true)]
also add together next in web.config
<webservices> <protocols> <add name="httpget"/> <add name="httppost"/> </protocols> </webservices>
c# jquery
No comments:
Post a Comment