This is application shows an example of calling a web service from ASP.NET AJAX web-service.
Here's what you will need to do:
1) Create a web service. This is a class which references the following namespaces in
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
namespace EliasNameSpace
{
[WebService (Namespace = "http://tempuri.org/" )]
[WebServiceBinding (ConformsTo = WsiProfiles .BasicProfile1_1)]
[System.Web.Script.Services. ScriptService ] //allow javascript calls public class TestService : System.Web.Services.WebService
{
[ WebMethod ]
public string HelloWorld()
{
return "Hello World" ;
}
}
}
2) Reference the web service in your page
< asp : ScriptManager ID ="ScriptManager1" runat ="server">
< Services >< asp : ServiceReference Path ="~/TestService.asmx" />Services >
asp : ScriptManager >
3) Call the webservice in your javascript code
function onSuccess(result, context, methodName) {
var str ="Success. Your result is: " + result;
str += "Context: " + context + "
"
+ "methodName: " + methodName + "" ;
$get("msgDiv" ).innerHTML += str;
}
function onFail(errror, context, methodName) {
var str = "Failed. Error: " + errror;
str += "Context: " + context + "
"
+ "methodName: " + methodName + "" ;
$get("msgDiv" ).innerHTML += str;
}
//this calls the webservice, onSuccess is the function called if it succeeded, onFail is called if it failed.
EliasNameSpace.TestService.HelloWorld(onSuccess, onFail);
If you'd like a working example:
Download Sample Ajaxed Web Service Application