- Right click on any folder you want to add the service to it and type the name of it.
- After you hit OK, your code should locks like the following
namespace ...
{
///
/// Summary description for WebService1
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow
this Web Service to be called from script, using ASP.NET AJAX, uncomment the
following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]// This to make this method accessible to public and access the web session in the link
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]// to return the text as json
public string
HelloWorld(string x1, int x2)
{
return "Hello World";
}
}
}
- To call HelloWorld method from jquery use the following code snippet
var x1='xxx', x2=20;
$.ajax({
type: "POST",
url: "/WebService1.asmx/HelloWorld",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{x1:'" + x1+ "', x2:'" + x2+ "'}",
beforeSend: function () {
$('#loadingImage').show();//to show a loading image while ajax call in progress
},
success: function (data) {
console.info(data.d);
},
complete: function () {
$('#loadingImage').hide();
},
error: function (XMLHttpRequest, result,
errorThrown) {
console.info('error',errorThrown);
}
});