c# - Unable to call aspx page web method using jquery ajax call? -
here ajax call
$(document).ready(function () { $("#btnsubmit").click(function () { alert("i in ?"); $.ajax({ type: "post", url: "testnew2.aspx/displaydata", data: "{}", contenttype: "application/x-www-form-urlencoded", datatype: "text", //success: function (msg) { // // replace div's content page method's return. // $("#btnsubmit").text(msg.d); // alert(msg.d); //} success: function (result, status, xhr) { document.getelementbyid("lbloutput").innerhtml = xhr.responsetext }, error: function (xhr, status, error) { alert(xhr.error); } }); }); });
and web method[webmethod] public static string displaydata() { return datetime.now.tostring(); }
getting aspx page when trying call web method on aspx page.here jquery code can 1 point out may wrong.because web method not getting called.
try like
$.ajax ({ url: " url", data: "{ 'name' : 'data'}", datatype: "json", type: "post", contenttype: "application/json; charset=utf-8", async: true, datafilter: function (data) { return data; }, success: function (data) { alert(data); }, error: function (xmlhttprequest, textstatus, errorthrown) { alert("error"); } });
or
jquery.ajax({ type: "post", url: "login.aspx/checkusernameavail", contenttype: "application/json; charset=utf-8", data: "{'iuser':'" + userid + "'}", datatype: "xml", success: function (msg) { $(msg).find("table").each(function () { var username = $(this).find('username').text(); if (username != '') { //window.location.replace('/icalendar.aspx'); alert('this username taken..'); $("#reguser").val(''); $("#reguser").focus(); } else { } }); }, error: function (d) { } });
.cs
[webmethod(enablesession: true)] [scriptmethod(responseformat = responseformat.xml)] public static string checkusernameavail(string iuser) { try { icalendarclass ic = new icalendarclass(); dataset ds = ic.checkusernameavail(iuser); return (ds.getxml()); } catch { return null; } }
Comments
Post a Comment