You can access service method through ajax calling
[System.Web.Script.Services.ScriptService]
public class DummyService : System.Web.Services.WebService
{
[WebMethod]
public string DummyMethod(string id)
{
try
{
return id
}
catch
{
return null;
}
}
[WebMethod]
public DummyModel DummyModelMethod(string id, string name)
{
try
{
return new DummyModel{Id=int.parse(id), Name=name};
}
catch
{
return null;
}
}
}
public class DummyModel
{
public int Id{set;get;}
public int Name{set;get;}
}
Ajax calling helper
function jqAjaxPost(webmethod, paramArray, successFn, errorFn) {
var paramList = '';
if (paramArray.length > 0) {
for (var i = 0; i < paramArray.length; i += 2) {
if (paramList.length > 0)
paramList += ',';
paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
}
}
paramList = '{' + paramList + '}';
$.ajax({
type: "POST",
url: webmethod,
contentType: "application/json; charset=utf-8",
data: paramList,
dataType: "json",
success: successFn,
error: errorFn
});
}
we can invoke the service method by following way
<script type="text/javascript">
jqAjaxPost("DummyService.asmx/DummyMethod",
["id", "1"],
function (msg) {
if (msg.d == null) {
alert("service error.");
return;
}
alert(msg.d);
},
function () {
alert('service method not found');
});
jqAjaxPost("DummyService.asmx/DummyModelMethod",
["id", "1", "name", "hoho"],
function (msg) {
if (msg.d == null) {
alert("service error.");
return;
}
alert(msg.d.Id);
alert(msg.d.Name);
},
function () {
alert('service method not found');
});
</script>